Skip to main content

PowerShell

PowerShell is a great tool, with so many commands it can be easy to forget some of the more niche and less used options. This document serves as a resource for handy and notable PowerShell commands.

PowerShell Core (PowerShell 7)

It may be of interest to upgrade to PowerShell Core from PowerShell as the latest version and final version of PowerShell is version 5.1, the development of PowerShell has since moved to PowerShell Core which is now on version 7. This does not come pre-installed and must be downloaded in order to use from the GitHub repository.

Check Installed Version:

$PSVersionTable.PSVersion

Search for the latest PowerShell version with winget:

winget search Microsoft.PowerShell

Install the latest PowerShell version with winget:

winget install --id Microsoft.Powershell --source winget

If the above does not work, download the .msi from the GitHub Repository: https://github.com/PowerShell/PowerShell

Set Execution Policy

For when a script needs to be run outside the Default Execution Policy:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted 

To return to the Default Policy use:

Set-ExecutionPolicy -ExecutionPolicy Default

Exchange Online Management

Installation:

Install-Module ExchangeOnlineManagement 

Connection:

Connect-ExchangeOnline -UserPrincipalName admin@domain.com

Check Calendar Permissions:

Get-MailboxFolderPermission -Identity "your_user:\Calendar"

Active Directory Commands

View Commands:

Get-Command -Module ActiveDirectory

View Domain Information:

Get-ADDomain

View Domain Controllers by Hostname and Operating System

Get-ADDomainController -filter * | select hostname, operatingsystem

View Fine Grained Password Policies

Get-ADFineGrainedPasswordPolicy -filter *

View Domain Default Password Policy:

Get-ADDefaultDomainPasswordPolicy

View All User Properties:

Get-ADUser username -Properties *

Search for User with Similar Name:

Get-ADUser -Filter {name -like "*name*"}

View Locked User Accounts:

Search-ADAccount -LockedOut

Unlock User Account:

Unlock-ADAccount –Identity user

View All Security Group Members:

Get-ADGroupMember -identity “security_group”

View All Security Groups:

Get-ADGroup -filter *

Add User to Security Group

Add-ADGroupMember -Identity group_name -Members user_0, user_1

Search for Group with Similar Name:

Get-ADGroup -filter * | Where-Object {$_.name -like "*name*"}

Group Policy

View Commands:

Get-Command -Module GroupPolicy

View GPO's with Status:

Get-GPO -all | select DisplayName, gpostatus

Remote PowerShell Sessions

Create a session only if a connection test succeeds:

if (Test-Connection -TargetName your_server -Quiet) { New-PSSession -ComputerName your_server }

End session with:

Exit-PSSession