Powershell cheatsheet

Bypass digitally sign constrain for scripts execution

Run powershell as Administrator.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

List environment variables

Get-ChildItem -Path Env:\

Also, using .net from powershell, and specifying system-level variables, by specifying machine as second argument. This argument could also be user

[System.Environment]::GetEnvironmentVariable('VARIABLE_NAME','machine')

For some reason, the previous doesn’t work for variable COMPUTERNAME. To get computer name:

$env:computername

Set environment variable

Run powershell as Admin.

[System.Environment]::SetEnvironmentVariable('VARIABLE_NAME', 'VARIABLE_VALUE',[System.EnvironmentVariableTarget]::Machine)
$current=[System.Environment]::GetEnvironmentVariable('PATH','machine');$newpath="$current;C:\new\stuff\in\the\path";[System.Environment]::SetEnvironmentVariable('PATH', $newpath,[System.EnvironmentVariableTarget]::Machine)

Remove environment variable

Remove-Item env:\VARIABLE_NAME

Enable windows feature (ServerManager)

Get-WindowsCapability -Name RSAT* -Online | Add-WindowsCapability -Online

Takes several minutes.

Import-Module ServerManager
Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -ComputerName $env:computername -WhatIf

Create file

New-Item C:\path\to\file.txt

Create folder

New-Item -Path "c:\newfolder\anothernew" -Name "lastnewfolder" -ItemType "directory"

Creates the full path c:\newfolder\anothernew\lastnewfolder.

Rename file or folder

Rename-Item .\folderName -NewName NEW_FOLDER_NAME

Same for files.

Move folder

Move folder FOLDER_PATH and all its content into folder DESTINATION_FOLDER_PATH.

Move-Item FOLDER_PATH -destination DESTINATION_FOLDER_PATH

Delete folder

rm -r -fo .\folderName

-r: -recurse -fo: -force

Set content to a file

set-content .\file.txt -value "new content to file"

Get content of a file (equivalent to linux cat)

type .\file.txt

Grant a user full permission over a folder

icacls "C:\folder\to\set\permissions\to" /inheritance:d /grant:r "[USER_NAME]:(OI)(CI)F" /T

Set registry value

Set-ItemProperty -path 'HKLM:\path\to\key\' -name [KEY_NAME] -value [KEY_VALUE]
# e.g.
Set-ItemProperty -path 'HKLM:\software\microsoft\microsoft sql server\mssql15.MSSQLSERVER\mssqlserver\' -name LoginMode -value 2

Get local harddrives storage info

Further info in https://mcpmag.com/articles/2018/01/26/view-drive-information-with-powershell.aspx

# all
Get-WmiObject -Class Win32_logicaldisk

# local
Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType = '3'"

Remove alias (e.g. cURL)

rm alias:curl

Extract zip file

Expand-Archive -Path file.Zip -DestinationPath C:\Reference

Download and run msi installer

Example with az cli installer

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi

Disable annoying sound in powershell

Run as admin

Set-Service beep -StartupType disabled