Skip to content

Instantly share code, notes, and snippets.

@modesto
Last active March 16, 2021 17:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save modesto/1d8c254cdb30e66038dcfd2843eec7e5 to your computer and use it in GitHub Desktop.
Save modesto/1d8c254cdb30e66038dcfd2843eec7e5 to your computer and use it in GitHub Desktop.
PowerShell-CheatSheet

Memory allocated for each proccess:

get-process | Group-Object -Property ProcessName | Format-table Name, @{n='Mem (KB)';e={'{0:N0}' -f (($_.Group|Measure-Object WorkingSet -Sum).Sum / 1KB)};a='right'} -AutoSize

Get Temp folder:

$env:TEMP  # 8+3 format
[System.IO.Path]::GetTempPath() # Long path format

Aliases:

Get-Alias # All
Get-Alias -Definition Get-ChildItem # By command
$alias:dir # Get alias source of 'dir'

Declared variables:

ls variable: # Get all variables
ls variable:*xxx* # Get all variables that contains xxx in the name
ls variable: | Out-String -stream | Select-String "C:\\" # Get all variables than contains C:\ in the value
del variable:xxx # Delete variable with name xxx. Use with -force if its write-protected
New-Variable anyVariableName -value "any value" -description "any description" -option Constant # Create a constant

Requires (need to be the first staments in a script:

#requires -Modules PrintManagement 
#requires -Version 3 
#Requires -RunAsAdministrator

Network command :

Get-NetIPConfiguration
Get-NetAdapter

Disable-NetAdapter -Name "xxx"
Enable-NetAdapter -Name "xxx"
Rename-NetAdapter -Name "xxx" -NewName "xxx2"

Get-NetAdapter -Name "Local Area Connection" | Get-NetIPAddress
New-NetIPAddress -InterfaceAlias "Wireless" -IPv4Address 10.0.1.95 -PrefixLength "24" -DefaultGateway 10.0.1.1 # set new ip
Set-NetIPAddress -InterfaceAlias "Wireless" -IPv4Address 192.168.12.25 -PrefixLength "24" # change an existing IP
Set-DnsClientServerAddress -InterfaceAlias "Wireless" -ServerAddresses "10.10.20.1","10.10.20.2"
Set-NetIPInterface -InterfaceAlias "Wireless" -Dhcp Enabled

Test-NetConnection -ComputerName www.anyaddress.com # Ping
Test-NetConnection -ComputerName www.anyaddress.com -InformationLevel Detailed # Ping with detailed info
1..3 | Test-NetConnection -ComputerName www.anyaddress.com # Ping 3 times

Test-NetConnection www.anyaddress.com –TraceRoute

Test-NetConnection -ComputerName www.anyaddress.com -Port 80 # Port scan
Test-NetConnection -ComputerName www.anyaddress.com -CommonTCPPort HTTP # Port scan

Resolve-DnsName www.anyaddress.com # NSLookup
Resolve-DnsName www.anyaddress.com -Type MX -Server 8.8.8.8

Get-NetTCPConnection –State Established # NetStat

Launch process:

Start-Process -WindowStyle Minimized 'anyprocess.exe' # WindowStyle can be Hidden, Maximized or Normal

Text to speech:

Add-Type -AssemblyName System.Speech 
$synth = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$synth.Speak('Hello world') 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment