Powershell

Basic notes for powershell and its explanations

In Powershell, Get-Help, Get-Command, Get-Alias are the most handful commands to ever exist

Aliases are the shorthand notes for the commandlets, it lets to identify the correct commandlet of the specified alias

Get-Alias iwr
  • Know the propeties of a command

Get-NetTCPConnection | Get-Member
  • Reference

$_ --> refers to the element which is piped
  • Referncing with conditions

# ? --> alias to where 
# $_ --> refers to the ouptut of the Get-NetTCPConenction
# LocalAddress --> an members of the Get-NetTCPConnection cmdlet
Get-NetTCPConnection | ? {$_.LocalAddress -eq "127.0.0.1"}
Get-NetTCPConnection | Where-Object {$_.LocalAddress -eq "127.0.0.1"}
  • Only select contents whose contents are not empty

# $null --> value of the emptry string in powershell
Get-Process | ? {$_.Path -ne $null } | Select-Object path
  • Head / tail in powershell

# head
Get-Process | Get-Member | Select-Object -First 10
# tail
Get-Process | Get-Member | Select-Object -last 10
  • The gettype() is used to identify the output type from a command

  • Based on the type of output, we can query it accordingly

(whoami).gettype()
(ls).gettype()
  • Once the type is identified, the parameters that support the ouptut of the commandlet can be identified with Get-Member

whoami | Get-Member
  • The methods and properties of the specified commandlet can be accessed with (). operator

# accessing property
(whoami).length

# accessing mehtod
(whoami).ToUpper()
  • The commands can be nested or can be used as a subset with the ()

  • The Format-Table, Format-List, Format-Custom are common ways to show an output

(New-Object -com "Microsoft.Update.AutoUpdate").Results | Format-List
  • The 2>/dev/null equivalent of powershell is -ErrorAction

Get-ChildItem -ErrorAction 'SilentlyContinue'
  • ls -la of powershell Get-ChildItem -Force

Last updated