Skip to main content

PowerShell

Help

help equal Get-Help | more

Get-Help [<cmdlet>]
[ -ShowWindow ]
[ -Detailed | -Full | -Examples ]

Get command info

Get-Command <command>

Alias: gcm

PowerShell version

$PSVersionTable

List

<expression>[, ...]

Alias

Get-Alias [[-Name] <alias> | -Definition <cmdlet>]

Property

Get the properties and methods of objects

<cmdlet> | Get-Member

Alias: gm

Select property

<command> | Select-Object [[-Property] <property[]>]

Alias: select

Expand property

<command> | Select-Object -ExpandProperty <property>
(<command>).<property>

Create property

<command> | Select-Object @{N[ame]="<property name>"; E[xpression]={<expression>}}

Filter object

<command> | Where-Object [-FilterScript] { <Script> }

# Example
1, 2, 3, 4, 5, 6 | ? { $_ % 2 -eq 0 }
# 2 4 6

Alias: ?

Filter with file size

List file with file size > 2GB

ls | ? { $_.Length -gt 2gb }

PowerShell remoting

Enable PowerShell remoting

Enable-PSRemoting [-SkipNetworkProfileCheck] [-Force]

Trusted host

Get trusted host

winrm get winrm/config/client
Get-Item WSMan:\localhost\Client\TrustedHosts

Set trusted host

winrm set winrm/config/client @{TrustedHosts="<host>[,<host>]"}
# <host> = <IP> | <hostname> | *
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "<host>[,<host>]" [-Force]

Get credential from user input

Get-Credential [[-UserName] <username>]

PowerShell session

Create PowerShell session

$<variable> = New-PSSession
[[-ComputerName] <host[]>]
[-Credential { <user> | <credential> }]

Connect to remote PowerShell

Enter-PSSession
[-ComputerName] <host>
[-Credential { <user> | <credential> }]

Get drive

Get-PSDrive

Run command

Referense

Invoke-Command [[-ComputerName] <host[]>]
[-ScriptBlock] <ScriptBlock>
[-Credential <PSCredential>]
[-ArgumentList <argument[]>]
& <ScriptBlock> [<argument[]>]

Execution policies

Ref: about_Execution_Policies

Get-ExecutionPolicy
Set-ExecutionPolicy <policy>

Unblock script

Unblock script that was downloaded from the Internet

Unblock-File <path>

History

Get-History

Alias: history

Search history

Ctrl + R

Get all session history

Get-Content (Get-PSReadlineOption).HistorySavePath

Hash table

Ref: about_Hash_Tables

$hash = @{
<name> = <value>;
[<name> = <value>] ...
}

List keys

$hash.Keys

List values

$hash.Values

Add

$hash[<key>] = <value>
$hash.Add(<key>, <value>)

Remove

$hash.Remove(<key>)

Path

Get script path

$Script:MyInvocation.MyCommand.Path

Get file name of path

Split-Path -Leaf <path>

Get folder of path

Split-Path <path>

Join path

Join-Path <path> <path> ...

Get system directory

[System.Environment]::SystemDirectory

List Windows Features

Get-WindowsOptionalFeature -Online

Clear MRU

MRU (Most Recently Used)

sp "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU\" -Name MRUList -Type String -Value ""

Get route for IP address

Find-NetRoute -RemoteIPAddress <IP address>

Multi-Line

<command> `
<parameter> ...

Run without / hide console

powershell.exe -WindowStyle Hidden -Command "<command>"

Function

Parameter

function <name> [([type]$param1 [,[type]$param2])]
{
<statement list>
}

function <name>
{
param([type]$param1 [,[type]$param2])
<statement list>
}
function Add([int]$a, [int]$b) {
return $a + $b
}