Skip to content

Instantly share code, notes, and snippets.

@andykuszyk
Last active October 13, 2016 09:20
Show Gist options
  • Save andykuszyk/fa9e18259e58e4babb7c53ed8c0054a2 to your computer and use it in GitHub Desktop.
Save andykuszyk/fa9e18259e58e4babb7c53ed8c0054a2 to your computer and use it in GitHub Desktop.
Common Powershell commands

A collection of common Powershell commands that I frequently use.

Outputting to the console

Write to the console with:

Write-Host "My message"

Bypass the execution policy for the current user

To execute scripts in a powershell console, use:

Set-ExecutionPolicy -Scope CurrentUser Bypass

Simple find and replace

To do a simple find and replace in a file, use:

(Get-Content [filename] -encoding UTF8) | foreach-object {
	$_ -replace "[text-to-replace]", "[replacemet-text]"
} | Set-Content [filename] -encoding UTF8

Note: the brackets around Get-Content are important here, because they cause powershell to read the entire file out first, before starting to write it again.

Read an XML file

[xml]$xmlFile = Get-Content -Path $xmlFilePath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment