Skip to content

Instantly share code, notes, and snippets.

@trashbat
Last active October 14, 2019 11:19
Show Gist options
  • Save trashbat/a019ba7ffa7e28e8d097c1206aa72b42 to your computer and use it in GitHub Desktop.
Save trashbat/a019ba7ffa7e28e8d097c1206aa72b42 to your computer and use it in GitHub Desktop.
PowerShell Learnings

Getting help for a command, e.g. Get-ChildItem:

Get-Help Get-ChildItem

Using behind corporate proxy - suggest putting in your profile script (see below):

[System.Net.WebRequest]::DefaultWebProxy = New-Object System.Net.WebProxy('<address>:<port>')
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[System.Net.WebRequest]::DefaultWebProxy.BypassProxyOnLocal = $true

Registering the PSGallery repository:

Register-PSRepository -Default
Get-PSRepository

Which version are we running?

$PSVersionTable

Create new profile script:

New-Item $Profile -Type File -Force

Reload profile script:

Invoke-Expression $Profile
# Or use the alias:
iex $Profile

Check if path exists:

Test-Path <path>

List aliases:

Get-Alias

List commands:

Get-Command

List modules:

Get-Module
Get-Module -ListAvailable

Install a module, e.g. Azure:

Install-Module -Name Azure -Repository PSGallery -Force
# Run `Get-Module -ListAvailable` after it's installed to see all related modules.
Import-Module AzureRM.profile
Import-Module Azure.Storage
Import-Module Azure
# Now `Get-Module` (on its own) shows the Azure modules you just imported.

Pipelines and object filtering:

Get-Module | Where-Object { $_.Name -Match "^Azure.*" } | Remove-Module

Managing output:

Get-Command | Out-Host # The default, same as running `Get-Command` with no piping
Get-Command | Out-Host -Paging
<some command> | Out-Null # Analogous to sending output to /dev/null
Get-Command | Out-File ".\commands.txt" # Must specify the "."

Showing specific properties:

(Get-Command).Name # For single property
Get-Command | Select-Object -Property Name # Does the same
Get-Command | Select-Object -Property Name,Source # For multiple properties

Getting and setting environment variables:

$env:FOO # getting
$env:FOO = "bar" # setting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment