User Tools

Site Tools


program_lang:powershell_misc

This is an old revision of the document!


Powershell misc

Powershell man page

List all commands, you can add as parameter -examples -full -detailed

PS /home/manu> get-command 
...
PS /home/manu> get-command *service*

List all commands from a specific module

PS /home/manu> get-command -module activedirectory

Man page

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>]

Commands alias

PS /home/manu> get-alias

Upper / Lower

PS /root> $text1 = "Hello".ToUpper()
PS /root> Write-Host $text1
HELLO
PS /root> $text1 = "Hello".ToLower()
PS /root> Write-Host $text1
hello

ATTENTION: Powershell is not case sensitive

PS /root> $text1 = "Hello"
PS /root> $text2 = "HELLO"
PS /root> $text1 –eq $text2
True

List full parameters

Get full parameters format-list or fl

PS /root> Get-Process | format-list
Id      : 1627
Handles : 7
CPU     : 0
SI      : 1627
Name    : xrdp-sesman

...

Get selected parameters

PS /root> Get-Process | format-list -Property Id,CPU
Id  : 1627
CPU : 0

...

Powershell tail, sort

PS /root> Get-Process | Sort-Object -Property CPU | Select-Object -Last 5                                                                                                                                    

 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName                                                                                                                                                       
 ------    -----      -----     ------      --  -- -----------                                                                                                                                                       
      0     0.00    1695.88    3461.67    2118 118 X                                                                                                                                                                 
      0     0.00      83.30    3837.19    5662 328 chrome                                                                                                                                                            
      0     0.00     562.67    4124.23    3965 283 firefox                                                                                                                                                           
      0     0.00      20.55    5235.46    2373 370 pulseaudio                                                                                                                                                        
      0     0.00    6483.15   79748.30   31295 264 VirtualBoxVM  

log your script

$Logfile = $MyInvocation.MyCommand.Path -replace '\.ps1$', '.log'

Start-Transcript -Path $Logfile
#Doing some stuff with the Verbose parameter
Get-ChildItem -Verbose
Get-Service -Verbose
Get-Process -Verbose
Write-Output 'Writing some text to the log file' 

Stop-Transcript

Suppress multiple pattern in list

PS /usr/fcm> $Eliminate = @("vml","vm2","vm3")
PS /usr/fcm>  Get-VM | Select-String -pattern $Eliminate  -notMatch
vm4
vm5

sed in Powershell

# cat input.txt | sed 's/old/new/' > output.txt
  cat: in Powershell : Get-Content
  $_ : pipe
  { } : command delimiter
  % : iteration
PS> Get-Content input.txt | %{$_ -replace "old", "new"} | Set-Content output.txt

sort in Powershell

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
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
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-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
| Sort-Object -Unique : only one occurrence of each word
| Sort-Object : string sort
| Sort-Object {[int]$_} : integer sort
-CaseSensitive ...
-Descending
program_lang/powershell_misc.1640976433.txt.gz · Last modified: 2021/12/31 19:47 by manu