Usefull PowerShell commands

PowerShell is a powerfull tool when administrating Microsoft products and personally i like using Windows PowerShell ISE that is an powershell tool provided from Microsoft.
It can be found on Windows 8, Windows Server 2012, Windows Server 2008 R2 and Windows Server 2008.

Here are som usefull PS one-liners that i use often 🙂

# Connect to remote server
Enter-PSSession “yourdomaincontroller”

# Disconnect remote session
exit-pssession

# Import the ActiveDirectory cmdlets
Import-Module ActiveDirectory

# List available snapins on your system
Get-PSSnapin

# Add snapins examples
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin # Exchange 2007
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 # Exchange 2010
Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager # WMM (Hyper-V)
Add-PSSnapin Quest.Activeroles.ADManagement # Quest commandlets

# List registered snapins
Get-PSSnapin -Registered

# List Active Directory Computers that have never logged in within time span (-TimeSpan 365.00:00:00, this is 365 days), first line counts the result, second line shows the result, line three sends output to an txt file.
(Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 35.00:00:00 | select name | sort-object name).count
Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 90.00:00:00 | select name | sort-object name
Search-ADAccount -AccountInactive -ComputersOnly -TimeSpan 65.00:00:00 | select name | sort-object name | out-file c:\temp\computers.txt

# The following example demonstrates how to find user accounts that have been inactive for 90 days:
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 | where {$_.ObjectClass -eq ‘user’} | FT Name,ObjectClass –A

# The following example demonstrates how to find inactive user accounts
Search-ADAccount -AccountInactive | where {$_.ObjectClass -eq ‘user’} | FT Name,ObjectClass –A

# The following example demonstrates how to find user accounts that have been inactive since 01/01/2013
Search-ADAccount -AccountInactive -DateTime 01/04/2013 | where {$_.ObjectClass -eq ‘user’} | FT Name,ObjectClass –A

# The following example demonstrates how to find locked-out users in your domain
Search-ADAccount -LockedOut | where {$_.ObjectClass -eq ‘user’} | FT Name,ObjectClass -A

#The following example demonstrates how to unlock the user account U1 in the organizational unit (OU) Test in your domain.
Unlock-ADAccount -Identity “CN=U1,OU=Test,DC=FABRIKAM,DC=COM”

# The following example demonstrates how to unlock the user account U1 in your domain.
Unlock-ADAccount -Identity U1

# Reset pasword on spesific user where “bob” is changed to the username of the user you want to change password on
Set-ADAccountPassword -Identity bob -Reset