PowerShell Cheat Sheet (3) - Functions, Error Handling, Files Apr 7, 2013 Functions # Defining function Get-Length($text) { Write-Hist $text.Length } # Executing Get-Length "Lukasz" # Passing reference function Change-Value([ref] $x) { $x = 5; } # Executing $x = 1 Change-Value ([ref]$x) # Functions with pipeline function Proc-Pipeline() { begin {} process {} end {} } # Filters filter Filter-Files { if($_.Name -like "*.cs") { return $_ } } # Support for switches function Get-Help () { param([switch]$x, [switch]$y) if($x.IsPresent) { } } Error Handling # Trap function With-Trap($x, $y) { $z = $x/$y trap [System.DivideByZeroException] { } trap { Write-Host $_.ErrorID Write-Host $_.Exception.Message continue # break } } Files # Getting the content $content = Get-Content "file.txt" # Getting lines $content[0] $content[1] # Joining $content_as_string = [String]::Join($sep, $content) # Wildcard support $content = Get-Content "*.txt" # Writing to file (with overwrite) Set-Content -Value "Text to file" -Path "file.txt" # Append to file Add-Content -Value "Some more text" -Path "file.txt" # Remove file Remove-Item "file.txt" # CSV files Export-Csv $header = "Col1", "Col2" $content = Import-Csv "File.csv" -Header $header # XML files - write $content = "<html><body>Body text</body></html>" $content | Out-File "file.xml" # XML files - read $content = New-Object xml $content.Load("file.xml") # Read data $body_text = (@(content.html.body)[0]).Clone()