assorted PowerShell recipes

See also: all the recipes and the intro

# setting up the powershell remoting
# setting the limits for powershell remoting

# Encrypting with RSA in PowerShell
# encrypting with RSA

# custom powershell object

# .NET/PowerShell string formatting

# PowerShell function parameters

# PowerShell classes

# ArgumentTransformationAttribute – custom type casts for the cmdlet arguments

# PowerShell issues on Github

# Calling REST from full PowerShell

# Get all possible values of an enum class
# Build an Enum object from its int value
[Enum]::Parse([System.Security.AccessControl.FileSystemRights], 1)

# Finding the special paths from PowerShell (with enum Environment.SpecialFolder)
# (translates to SHGetKnownFolderPath())

# check in powershell if the user is elevated
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”)

# Convert a base64 string to byte array
$bytes = [Convert]::FromBase64String(“ABCDEFGH”)
# convert the byte array to Unicode
# to encode Unicode as bytes

# Translate a windows error code to message in PowerShell

# Catch a custom exception thrown as a string
catch [System.Management.Automation.RuntimeException]

# check the value for being not $null (note: NO () after ToString)
if ($msg.ToString) { “zzz” }

# Translate the host name to adress (DNS lookup)
# Translate for IPv4 only
[System.Net.Dns]::GetHostEntry($Name).AddressList | ? {$_.AddressFamily -eq “InterNetwork”} | % {$_.IPAddressToString}
# easier with new PowerShell cmdlet but your luck with the PTR records may vary,
# they might not get automatically recursively resolved
@((Resolve-DnsName “host”).IP4Address)[0]
@((Resolve-DnsName “host”).IP6Address)[0]

# set the DNS configuration with PowerShell
Set-DnsClientServerAddress -InterfaceAlias “vEthernet (Mgmt)” -ServerAddresses @(“”)
set-dnsclient -ConnectionSpecificSuffix “” -interfacealias “vEthernet (Mgmt)”

# Get the event session information
$EventSession = [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession;
$prov = New-Object -TypeName System.Diagnostics.Eventing.Reader.ProviderMetadata -ArgumentList “Microsoft-Windows-BootEvent-Test-EvtGen”
$prov.Id # the GUID

# Catch all errors except the language errors
try {
throw zzz
} catch
} catch {
$e = $_

# l18n for PS scripts

# Copy files to a remote machine (Win10+)
Copy -ToSession $s …
# Copy files from a remote machine (Win10+)
Copy -FromSession $s …

# Create a custom PSObject
[PsCustomObject]@{ a=1; b=2; }

# remember the password credentials, password entered from keyboard
$cr = get-credential Administrator

# making a secure string from plain-text password
$ss = ConvertTo-SecureString -AsPlainText “P4ssw0rd” -Force
# Extracting the value from a SecureString – shorter version
(New-Object System.Net.NetworkCredential “”,$ss).Password
# Extracting the value from a SecureString – longer version
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($ss)
$plain = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr)
# Making a PS credential with a secure string password
$cr = New-Object System.Management.Automation.PSCredential @(“Administrator”, $ss)
# Making a session
$sess = new-pssession -ComputerName -Credential $cr
# Running commands in a session
invoke-command -session $sess -ScriptBlock { bcdedit /enum }

# Create a script block from a string
$z = [ScriptBlock]::Create($sb.ToString())

# escape the regular expression characters
$str = [regex]::Escape($str)
# escape the XML characters

# Get the tmp directory (per-user)
# Create a random file name (no directory part, really a random string)
$t = [System.IO.Path]::GetRandomFileName()
# Create cryptographically-random bytes (such as an AES encryption key)
$key = New-Object byte[](32)
$rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
# Generate a GUID
# garbage-collect, useful before unloading a hive and such

# Show the results as a GUI table
# as a GUI tree of object data

# Features needed to get VHD cmdlets from Hyper-V
dism /online /get-features | findstr “Hyper”
Microsoft-Hyper-V-Management-PowerShell /all
(and reboot afterwards)

# mount a share from powershell
New-PSDrive -Name Y -PSProvider FileSystem -Root \\host\shared -Credential domain\me

# How to discover the .NET APIs with PowerShell
$dll = [Reflection.Assembly]::LoadFile(“C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0.0\System.Runtime.Extensions.dll”)
$dll.GetExportedTypes() | ft FullName
$dll.GetTypes() | ft FullName
$dll.GetType(“System.Environment”).GetMembers() | ft
# iterate through multiple DLLs, collecting the type names in all of them
$types = foreach ($f in (dir “C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0.0\*.dll”)) { try { $dll = [Reflection.Assembly]::LoadFile($f.FullName); foreach ($n in ($dll.GetExportedTypes().FullName)) { $
f.FullName + ” ” + $n}; } catch {} }
$types | ? { $_ -match “Environment” }

# print the methods of a type
[System.Threading.Timer].GetMembers()  | % { $_.ToString() }


Comments (0)

Skip to main content