PowerShell Command-Line
A collection of PowerShell commands for Active Directory, Group Policy, Hyper-V, and Exchange Online administration.
This post contains PowerShell commands I frequently use for managing Active Directory, Group Policy, Hyper-V, and Microsoft 365.
Active Directory
Export All Active Directory Users to CSV
The following command exports all Active Directory users and selected properties to a CSV file:
1
2
3
Get-ADUser -Filter * -Properties * |
Select-Object Name, Mail, LastLogonDate, WhenCreated |
Export-Csv -Path "C:\userexport.csv" -NoTypeInformation
The exported file is saved as:
1
C:\userexport.csv
The -NoTypeInformation parameter prevents PowerShell type metadata from being added to the CSV file.
Get Active Directory User Properties
Retrieve all available properties for a specific Active Directory user:
1
Get-ADUser -Identity <ACCOUNT> -Properties *
Replace <ACCOUNT> with the user’s username, distinguished name, GUID, or another supported identity value.
For example:
1
Get-ADUser -Identity jdoe -Properties *
Get Group Policy Objects and Creation Times
Use dsquery to list Group Policy Objects and display their names and creation times:
1
dsquery * -filter "(ObjectCategory=groupPolicyContainer)" -attr displayName whenCreated
Note: Run this command from a system with the appropriate Active Directory tools installed.
Hyper-V
List Snapshots for a Virtual Machine
List the snapshots associated with a local virtual machine:
1
Get-VMSnapshot -VMName <VM_NAME>
To list snapshots for a virtual machine on a specific Hyper-V host:
1
Get-VMSnapshot -ComputerName <HYPER_V_HOST> -VMName <VM_NAME>
For example:
1
Get-VMSnapshot -ComputerName HV01 -VMName APP01
Delete All Snapshots
The following command removes all snapshots associated with a virtual machine:
1
2
Get-VMSnapshot -VMName <VM_NAME> |
Remove-VMSnapshot
Warning: Snapshot deletion can be irreversible and may trigger disk-merge operations. Confirm that the snapshots are no longer required before running this command.
To remove a snapshot from a remote Hyper-V host:
1
2
Get-VMSnapshot -ComputerName <HYPER_V_HOST> -VMName <VM_NAME> |
Remove-VMSnapshot
Microsoft 365 and Exchange Online
Connect to the Exchange Tenant
The following example creates a remote PowerShell session to Exchange Online:
1
2
3
4
5
6
7
8
9
10
$UserCredential = Get-Credential
$Session = New-PSSession `
-ConfigurationName Microsoft.Exchange `
-ConnectionUri "[https://outlook.office365.com/powershell-liveid/](https://outlook.office365.com/powershell-liveid/)" `
-Credential $UserCredential `
-Authentication Basic `
-AllowRedirection
Import-PSSession $Session -DisableNameChecking
When you finish working, remove the remote session:
1
Remove-PSSession $Session
Note: Basic authentication and the older remote PowerShell connection method shown above may no longer be available in many Microsoft 365 environments. Use the current Exchange Online PowerShell module and modern authentication for new deployments.
A typical modern connection uses the Exchange Online module:
1
Connect-ExchangeOnline
After installing the module, authenticate with your Microsoft 365 account when prompted.
Get Calendar Permissions
Display the permissions assigned to a user’s calendar:
1
Get-MailboxFolderPermission -Identity "<USER>:\Calendar"
For example:
1
Get-MailboxFolderPermission -Identity "[email protected]:\Calendar"
To display permissions for a shared mailbox calendar:
1
Get-MailboxFolderPermission -Identity "[email protected]:\Calendar"