Powershell cheat sheet

From Coolscript
Jump to navigation Jump to search


Execution Policy

get-ExecutionPolicy
set-ExecutionPolicy RemoteSigned
set-ExecutionPolicy unrestricted

Get Version

$psversiontable

Install AZ Module

Install-Module -Name AZ -AllowClobber -Scope CurrentUser
Import-Module  Az -Verbose

Connect AZ

Connect-AzAccount
Connect-AzAccount -device
connect-azaccount -UseDeviceAuthentication
  • List Subscriptions
Get-AzSubscription
Set-AzContext -Subscription "xxxx-xxxx-xxxx-xxxx"


Dump Environ

$environ=(gci env:*).GetEnumerator() | Sort-Object Name | Out-String
write-output $environ

Array

$allscopes = @() 
$cnt++
$allscopes += ,($cnt,"Val1-1","Val1-2","Val1-3") 
$cnt++
$allscopes += ,($cnt,"Val2-1","Val2-2","Val2-3") 
$cnt++
$allscopes += ,($cnt,"Val3-1","Val3-3","Val3-3") 


foreach ($row in @($allscopes)) {
 write-output "->$($row[0]) <--> $($row[1]) <--> $($row[2])  <--> $($row[3]) <-"
}


#Modify cell
for ( $index = 0; $index -lt $allscopes.count; $index++ )  {
   $allscopes[$index][0] = "X"
}
write-output "------------"
foreach ($row in @($allscopes)) {
 write-output "->$($row[0]) <--> $($row[1]) <--> $($row[2])  <--> $($row[3]) <-"
}


Date

Get epoche date

$EpochDate = Get-Date (Get-Date).ToUniversalTime() -UFormat %s

Convert

$date=(get-date).ToString("yyyy-MM-ddTHH:mm:ss")

Manual Module install

Online MAchine

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -Verbose

Check/Set Repo

Get-PSRepository
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted

Search or Install the module

Find-Module –Name *Posh-SSH*
Install-Module -Name Posh-SSH


  • Save Module
Save-Module –Name Posh-SSH –Path C:\temp\

Copy Module to the offline machine

Offline MAchine

-Check the module path and pick one

$env:PSModulePath -split ";"
Usually it is
c:\Program Files\WindowsPowerShell\Modules\
*Copy the module, 
*navigate into
*check for DLL's, 
*Properties
*UNBLOCK
Offline New Machine
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -Verbose
Import-Module Posh-SSH
Get-Command -Module Posh-SSH