Skip to main content

Posts

Showing posts from September, 2017

Useful Powershell stuff

Print a line
'_' * 100

Prevent null references, incorrect syntax usage, unnamed variables:
Set-StrictMode -Version Latest

Creating a new instance of a .net framework object
New-Object TypeName "System.Diagnostics.Stopwatch"
Or more simply, New-Object System.Diagnostics.Stopwatch
Because it's a string, don't use the type name in angle brackets like PS normally would

To view absolutely everything that's happening internally in the powershell pipeline for a particular scriptblock:
Trace-Command -Name * -Expression {Get-Service winrm | Restart-Service} -PSHost

Add a calculated property to a list of objects, along with all the other properties:
    Select *,@{Name="ExtraField"; Expression={$_.SomeValue+1}}, @{Name="Whatever"; Expression={"Some string"}}

Get a list of drives, including network drives and their UNC paths (like with "net use"):
    Get-WmiObject -Class win32_logicaldisk

Get previous session history with &qu…