This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
program_lang:powershell_misc [2021/12/05 21:10] manu [sed in Powershell] |
program_lang:powershell_misc [2022/11/18 17:45] (current) manu |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Powershell misc ====== | ====== Powershell misc ====== | ||
+ | |||
+ | ===== Powershell man page ===== | ||
+ | |||
+ | List all commands, you can add as parameter **-examples** **-full** **-detailed** | ||
+ | <cli prompt='>'> | ||
+ | PS /home/manu> get-command * | ||
+ | ... | ||
+ | PS /home/manu> get-command *service* | ||
+ | </cli> | ||
+ | |||
+ | List Get-Command cmdlet with an alias | ||
+ | <cli prompt='>'> | ||
+ | PS> Get-Command -Name dir | ||
+ | CommandType Name ModuleName | ||
+ | ----------- ---- ---------- | ||
+ | Alias dir -> Get-ChildItem | ||
+ | </cli> | ||
+ | |||
+ | List all commands from a specific module | ||
+ | <cli prompt='>'> | ||
+ | PS /home/manu> get-command -module activedirectory | ||
+ | </cli> | ||
+ | |||
+ | This command gets all of the cmdlets, sorts them alphabetically by the noun in the cmdlet name, and then displays them in noun-based groups: | ||
+ | <cli prompt='>'> | ||
+ | PS > Get-Command -Type Cmdlet | Sort-Object -Property Noun | Format-Table -GroupBy Noun | ||
+ | </cli> | ||
+ | |||
+ | uses the All parameter of the Get-Command cmdlet to show all instances of the Notepad command on the local computer. | ||
+ | <cli prompt='>'> | ||
+ | PS> Get-Command Notepad -All | Format-Table CommandType, Name, Definition | ||
+ | |||
+ | CommandType Name Definition | ||
+ | ----------- ---- ---------- | ||
+ | Application notepad.exe C:\WINDOWS\system32\notepad.exe | ||
+ | Application NOTEPAD.EXE C:\WINDOWS\NOTEPAD.EXE | ||
+ | </cli> | ||
+ | |||
+ | Man page | ||
+ | <cli prompt='>'> | ||
+ | PS /home/manu> get-help Write-Progress | ||
+ | |||
+ | NAME | ||
+ | Write-Progress | ||
+ | | ||
+ | SYNTAX | ||
+ | Write-Progress [-Activity] <string> [[-Status] <string>] [[-Id] <int>] [-PercentComplete <int>] [-SecondsRemaining <int>] [-CurrentOperation <string>] [-ParentId <int>] [-Completed] [-SourceId <int>] | ||
+ | [<CommonParameters>] | ||
+ | </cli> | ||
+ | |||
+ | Commands alias | ||
+ | <cli prompt='>'> | ||
+ | PS /home/manu> get-alias | ||
+ | </cli> | ||
+ | |||
+ | ===== Remove header/descriptor ===== | ||
+ | |||
+ | <cli prompt='>'> | ||
+ | PS /root> Get-VM lnx01a | ForEach-Object {$_.Name} | ||
+ | lnx01a | ||
+ | </cli> | ||
===== Upper / Lower ===== | ===== Upper / Lower ===== | ||
Line 78: | Line 139: | ||
vm4 | vm4 | ||
vm5 | vm5 | ||
+ | </cli> | ||
+ | |||
+ | ===== grep in Powershell ===== | ||
+ | |||
+ | <cli prompt='#'> | ||
+ | PS /usr/bin/fcm/tmp> Get-Content -Path vminfo.txt | Where-Object { $_ -Match "^VM_Datastore:"} | ||
</cli> | </cli> | ||
Line 95: | Line 162: | ||
</cli> | </cli> | ||
+ | ===== sort in Powershell ===== | ||
+ | <cli prompt='>'> | ||
+ | PS> Get-ChildItem -Path C:\Test | Sort-Object | ||
+ | Directory: C:\Test | ||
+ | Mode LastWriteTime Length Name | ||
+ | ---- ------------- ------ ---- | ||
+ | -a---- 2/13/2019 08:55 26 anotherfile.txt | ||
+ | -a---- 2/13/2019 13:26 20 Bfile.txt | ||
+ | -a---- 2/12/2019 15:40 118014 Command.txt | ||
+ | -a---- 2/1/2019 08:43 183 CreateTestFile.ps1 | ||
+ | d----- 2/25/2019 18:25 Files | ||
+ | </cli> | ||
+ | |||
+ | <cli prompt='>'> | ||
+ | PS> Get-ChildItem -Path C:\Test -File | Sort-Object -Property Length | ||
+ | Directory: C:\Test | ||
+ | Mode LastWriteTime Length Name | ||
+ | ---- ------------- ------ ---- | ||
+ | -a---- 2/13/2019 13:26 20 Bfile.txt | ||
+ | -a---- 2/12/2019 16:24 23 Zsystemlog.log | ||
+ | -a---- 2/13/2019 08:55 26 anotherfile.txt | ||
+ | -ar--- 2/12/2019 14:31 27 ReadOnlyFile.txt | ||
+ | -a---- 2/1/2019 08:43 183 CreateTestFile.ps1 | ||
+ | -a---- 2/12/2019 15:40 118014 Command.txt | ||
+ | </cli> | ||
+ | |||
+ | <cli prompt='>'> | ||
+ | PS> Get-Process | Sort-Object -Property WS | Select-Object -Last 5 | ||
+ | |||
+ | NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName | ||
+ | ------ ----- ----- ------ -- -- ----------- | ||
+ | 0 0.00 530.43 369.05 24977 283 firefox | ||
+ | 0 0.00 547.91 1360.88 4559 237 evolution | ||
+ | 0 0.00 1418.94 44.16 14488 237 okular | ||
+ | 0 0.00 2072.61 3317.51 2071 071 X | ||
+ | 0 0.00 6548.90 33021.09 6754 063 VirtualBoxVM | ||
+ | PS> Get-Process | ft name,starttime | ||
+ | xdg-document-po 25/12/21 07:49:32 | ||
+ | xdg-permission- 25/12/21 07:49:32 | ||
+ | xembedsniproxy 25/12/21 07:49:30 | ||
+ | xfs_mru_cache 25/12/21 07:45:06 | ||
+ | xfs-buf/sda4 25/12/21 07:45:06 | ||
+ | |||
+ | </cli> | ||
+ | |||
+ | <cli prompt='>'> | ||
+ | PS> Get-Service |Sort-Object -Property @{Expression = "Status"; Descending = $true},@{Expression = "DisplayName";Descending = $false} | ||
+ | |||
+ | Status Name DisplayName | ||
+ | ------ ---- ----------- | ||
+ | Running Appinfo Application Information | ||
+ | Running BthAvctpSvc AVCTP service | ||
+ | Running BrokerInfrastru... Background Tasks Infrastructure Ser... | ||
+ | Running BDESVC BitLocker Drive Encryption Service | ||
+ | Running CoreMessagingRe... CoreMessaging | ||
+ | Running VaultSvc Credential Manager | ||
+ | Running DsSvc Data Sharing Service | ||
+ | Running Dhcp DHCP Client | ||
+ | </cli> | ||
+ | |||
+ | | Sort-Object -Unique : only one occurrence of each word | ||
+ | | Sort-Object : string sort | ||
+ | | Sort-Object {[int]$_} : integer sort | ||
+ | -CaseSensitive ... | ||
+ | -Descending | ||
+ | |||
+ | ===== Misc commands ===== | ||
+ | |||
+ | ==== tail ==== | ||
+ | |||
+ | get last 5 lines from file | ||
+ | <cli prompt='>'> | ||
+ | PS> Get-Content -path /root/toto.py -tail 5 | ||
+ | |||
+ | PS> get-content |select -last 5 | ||
+ | |||
+ | PS> get-content |select -last 5 | Write-Output | ||
+ | </cli> | ||
+ | |||
+ | ==== head ==== | ||
+ | |||
+ | get first 5 lines from file | ||
+ | <cli prompt='>'> | ||
+ | PS> Get-Content -path /root/toto.py -totalcount 5 | ||
+ | </cli> |