PowershellSurvival

Basic Commands

Find file

Get-Childitem -Include *HSG* -File -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $FindDate }

Grep - Find File Content

Get-ChildItem -Filter *.log -Recurse | Select-String "Contoso"

Remove Directory and Contents

Remove-Item $folderPath -Force -Recurse -ErrorAction SilentlyContinue

List Environment Variables

gci Env:* | sort name

Set Environment Variable

$Env:AWS_REGION='eu-central-1'

XML

Load an XML document

[xml]$doc = Get-Content c:\temp\mystuff.nuspec -Encoding UTF8

Get element value

$id = $doc.package.metadata.id

Add an element with text value

$el = $doc.CreateElement('releaseNotes', $doc.package.metadata.NamespaceURI)
$el.InnerText = 'Fixed some bugs.'
$doc.package.metadata.AppendChild($el) | Out-Null   # Pipe to null to avoid dumping large output to console

Get default namespace of a document

$defaultNamespace = $doc.DocumentElement.NamespaceURI

Select nodes using namespace

[System.Xml.XmlNamespaceManager] $nsMgr = New-Object -TypeName System.Xml.XmlNamespaceManager -ArgumentList $xmlDoc.NameTable
$nsMgr.AddNamespace("ns", "http://schemas.microsoft.com/developer/msbuild/2003");
$rpNodes = $xmlDoc.SelectNodes("/ns:Project/ns:PropertyGroup/ns:RestorePackages", $nsMgr);

Modules

Import-Module .\MyModule.psm1
# use the module..., then:
Remove-Module MyModule

Pipeline

Given this function, saved in Get-CountX.ps1

[CmdletBinding()]
param(
    [Parameter(ValueFromPipeline)]
    [System.IO.FileInfo]$Path
)
begin {
    Write-Host 'Begin'
    $prefix = '-felix-'
}
process {
    Write-Host "$prefix $($Path.FullName.Length)"
}
end {
    Write-Host 'End'
}

...you can run this:

Get-ChildItem | .\Get-CountX.ps1

...and get a result like this:

Begin
-felix- 27
-felix- 37
-felix- 42
End

Or you can run this:

.\Get-CountX.ps1 -Path c:\temp\goofy.txt

...and get a result like this:

Begin
-felix- 17
End