Powershell簡介對比與常用命令

Windows PowerShell 是一種命令行外殼程序和腳本環境,使命令行用戶和腳本編寫者可以利用 .NET Framework 的強大功能和Linux命令。

通用方法:Win+R組合鍵打開運行命令,輸入PowerShell按回車就可以打開了
Win+X組合鍵(Win10中)
從cmd切換至PowerShell:可以在命令提示符中輸入PowerShell就可切換至Windows PowerShell了
從PowerShell切換至cmd:在PowerShell下輸入cmd就可以切回cmd了
簡單介紹

獲取所有命令:get-command

在這裏插入圖片描述

獲取所有進程:get-process

給指定命令重命名:Set-Alias,如Set-Alias aaa Get-Command

與cmd一樣,cls爲清屏命令

 幫助命令:help,首頁顯示不全,可以在--More--後按回車查看剩下的幫助信息

對比CMD

Cmd 只能支持傳統的 Windows 命令,既不能使用 .net 庫中的命令,也不能使用 Linux 下的命令。
在這裏插入圖片描述

但這方面 PowerShell 就強大多了,不僅完美支持傳統 Windows 命令和 .net 庫中的命令,也支持部分常用的 Linux 命令。

在這裏插入圖片描述

常用命令

Get-Command,Get-Help和Get-Member
在這裏插入圖片描述
1、快速掌握開始菜單項目數

想知道開始菜單所有的快捷方式數量,執行以下命令

Get-StartApps | measure
在這裏插入圖片描述

命令執行後,Count後面的數字,就是顯示出開始菜單快捷方式的數量

如果將該條命令的measure參數省略,單單執行Get-StartApps命令,可以獲取每個快捷方式的信息,包括其名稱和ID信息
在這裏插入圖片描述
查看開始菜單快捷方式數量的用途:可以方便我們統計開始菜單中快捷方式數量

2、一次性批量創建文件夾

利用PowerShell命令,可以創建以序列號編排的文件夾羣,例如,要在“E:999”文件夾中創建“實驗結果1”、“實驗結果2”……一直到“實驗結果20”共20個文件夾

PowerShell中執行切換目錄“E:”“CD 999”,切換到當前工作文件夾999,最後執行以下命令

MKDIR (1..20(1..20 | %{"實驗結果_"})

在這裏插入圖片描述
按照這樣操作後,就可以在999文件夾下自動建立上述20個文件夾

命令參數說明:MKDIR是建立文件夾的命令;數字串1…20表示文件夾的序號;“實驗結果”爲文件夾名稱前綴;$_表示序號,結果與前面所取的數字相一致。

查看類:

查看powershell版本 $PSVersionTable

查看操作系統版本信息 Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName . | Select-Object -Property Build*,OSType,ServicePack*

查看部分組策略 gpresult /Z

查看部分組策略(需要導出到文件) secedit /export /cfg c:\sec_result

查看服務 Get-Service

查看運行中服務 Get-Service | Where-Object {$_.Status -eq ‘Running’}

查看IP ipconfig /all或者Get-NetipConfiguration

查看進程 Get-Process

查看已安裝補丁 Get-WmiObject -Class Win32_QuickFixEngineering或者wmic qfe list

查看使用Windows Installer安裝的程序 Get-WmiObject -Class Win32_Product | Format-Wide -Column 1

查看CPU相關信息 get-wmiobject win32_processor

查看CPU使用率2008/2012通用 Get-WmiObject win32_processor | select SystemName, LoadPercentage

查看CPU使用率排名前20 Get-Counter -ComputerName localhost ‘\Process(*)% Processor Time’ | Select-Object -ExpandProperty countersamples | Select-Object -Property instancename, cookedvalue| Sort-Object -Property cookedvalue -Descending| Select-Object -First 20| ft InstanceName,@{L=‘CPU’;E={($_.Cookedvalue/100).toString(‘P’)}} -AutoSize

查看系統版本/序列號 gwmi win32_OperatingSystem

查看總內存 Get-WmiObject win32_OperatingSystem TotalVisibleMemorySize

查看總內存(單位GB) gwmi Win32_PhysicalMemory | %{$sum = 0} { $sum += KaTeX parse error: Expected 'EOF', got '}' at position 12: _.Capacity }̲ {Write-Host (sum / 1GB) “GB”}

查看空閒內存 Get-WmiObject win32_OperatingSystem FreePhysicalMemory

查看磁盤總空間(單位MB) Get-WMIObject Win32_LogicalDisk |Where-Object{$.Size}|Foreach-Object { ‘Disk {0} has {1:0.0} MB totalspace’ -f .Caption,(_.Caption, (.Size / 1MB) }

查看防火牆狀態 netsh advfirewall show currentprofile

查看BIOS信息 Get-WMIObject -Class Win32_BIOS

查看主板信息 Get-WMIObject -Class Win32_Baseboard

查看邏輯磁盤信息 Get-WMIObject -Class Win32_LogicalDisk

查看物理磁盤信息 Get-WMIObject -Class Win32_DiskDrive

查看桌面設置(屏保是否設置) Get-CimInstance -ClassName Win32_Desktop

查看一個文件夾內的文件及目錄 ​ ​ ​ ​ Get-ChildItem -Path C:\ -Force

管理類:

重啓服務器 Restart-Computer 或者 Restart-Computer -Force強制重啓

關閉服務器 stop-computer

停止spooler服務 Stop-Service -Name spooler

啓動spooler服務 Start-Service -Name spooler

重啓spooler服務 Restart-Service -Name spooler

停止某個進程 stop-process -id 2792

鎖定服務器 rundll32.exe user32.dll,LockWorkStation

新增註冊表項 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​New-Item -Path hkcu:\software_DeleteMe

刪除註冊表項 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​Remove-Item -Path hkcu:\Software_DeleteMe
————————————————

一、Get類
1.Get-Command : 得到所有PowerShell命令,獲取有關 cmdlet 以及有關 Windows PowerShell 命令的其他元素的基本信息。
包括Cmdlet、Alias、Function。
2.Get-Process : 獲取所有進程
3.Get-Help : 顯示有關 Windows PowerShell 命令和概念的信息
4.Get-History : 獲取在當前會話中輸入的命令的列表
5.Get-Job : 獲取在當前會話中運行的 Windows PowerShell 後臺作業
6.Get-FormatData : 獲取當前會話中的格式數據
7.Get-Event : 獲取事件隊列中的事件
8.Get-Alias : 獲取當前會話的別名
9.Get-Culture :獲取操作系統中設置的當前區域性

Get-Date :獲取當前日期和時間
Get-Host : 獲取表示當前主機程序的對象
12.Get-Member : 獲取對象的屬性和方法。
如:$var = 3
$var | get-member
結果:TypeName: System.Int32
Name MemberType Definition
---- ---------- ----------
CompareTo Method int CompareTo(System.Object value), int CompareTo(int value)
Equals Method bool Equals(System.Object obj), bool Equals(int obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
ToString Method string ToString(), string ToString(string format), string ToString(System.IFormatProvider provider…
13.Get-Random : 從集合中獲取隨機數或隨機選擇對象
14.Get-UICulture : 獲取操作系統中當前用戶界面 (UI) 區域性設置
15.Get-Unique : 從排序列表返回唯一項目
16.Get-Variable :獲取當前控制檯中的變量
17.Get-EventLog : 獲取本地或遠程計算機上的事件日誌或事件日誌列表中的事件
18.Get-ChildItem : 獲取一個或多個指定位置中的項和子項
19.Get-Content : 獲取指定位置的項的內容
20.Get-ItemProperty :獲取指定項的屬性
21.Get-WmiObject : 獲取 Windows Management Instrumentation (WMI) 類的實例或可用類的相關信息
22.Get-Location :獲取當前工作位置的相關信息(如:F:\Users\TaoMin )
23.Get-PSDrive:獲取當前會話中的 Windows PowerShell 驅動器
24.Get-Item:獲取位於指定位置的項
25.Get-Process :獲取在本地計算機或遠程計算機上運行的進程
26.Get-Service : 獲取本地或遠程計算機上的服務
27.Get-Transaction :獲取當前(活動)事務
28.Get-ExecutionPolicy :獲取當前會話中的執行策略

二.Set類 (set類命令一般都含有參數)
1.Set-Alias : 在當前 Windows PowerShell 會話中爲 cmdlet 或其他命令元素創建或更改別名(替代名稱)
如:如:Set-Alias aaa Get-Command
2.Set-PSDebug :打開和關閉腳本調試功能,設置跟蹤級別並切換 strict 模式
3.Set-StrictMode :建立和強制執行表達式、腳本和腳本塊中的編碼規則
4.Set-Date :將計算機上的系統時間更改爲指定的時間
5.Set-Variable :設置變量的值,如果該變量還不存在,則創建該變量
6.Set-PSBreakpoint :在行、命令或者變量上設置斷點
7.Set-Location :將當前工作位置設置爲指定的位置
8.Set-Item :將項的值更改爲命令中指定的值
9.Set-Service :啓動、停止和掛起服務並更改服務的屬性
10.Set-Content :在項中寫入內容或用新內容替換其中的內容
11.Set-ItemProperty :創建或更改某一項的屬性值
12.Set-WmiInstance :創建或更新現有 Windows Management Instrumentation (WMI) 類的實例
13.Set-ExecutionPolicy :更改 Windows PowerShell 執行策略的用戶首選項。

三.Write類
1.Write-Host : 將自定義輸出內容寫入主機。類似於.net的 write()或者writeline()功能
2.Write-Progress :在 Windows PowerShell 命令窗口內顯示進度欄
3.Write-Debug :將調試消息寫入控制檯
4.Write-Verbose:將文本寫入詳細消息流
5.Write-Warning :寫入警告消息
6.Write-Error : 將對象寫入錯誤流
7.Write-Output : 將指定對象發送到管道中的下一個命令;如果該命令是管道中的最後一個命令,則在控制檯上顯示這些對象
8.Write-EventLog :將事件寫入事件日誌

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章