Collection of Useful PowerShell Commands

This post contains a collection of PowerShell commands that I find useful in my day-to-day duties. I’ll update the post over time to include more commands.

Active Directory

List inactive User Accounts

This command uses the lastLogonTimestamp attribute to find user accounts that have not logged into the domain in the last 90 days. There are a few things to keep in mind when using the lastLogonTimestamp attribute which you can read about here.

$date = (Get-Date).AddDays(-180)
# Get all AD computers with lastLogonTimestamp less than our time
Get-ADUser -Filter {LastLogonTimeStamp -lt $date} -Properties LastLogonTimeStamp | Sort-Object Name | `
	Select-Object Name,SamAccountName,@{Name="Timestamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}

DISM

Convert ESD to WIM

This will convert all images in the ESD to the WIM file.

PS> Get-WindowsImage -ImagePath "D:\sources\install.esd" | %{ Export-WindowsImage -SourceImagePath $_.ImagePath -SourceIndex $_.ImageIndex -CheckIntegrity -CompressionType Max -DestinationImagePath "C:\data\install.wim" }
Author image
About Jacob Hodges