PowerShell Handy Commands
Some helpful commands for PowerShell Beginner.
Note
Some Commands/Scripts require Powershell version 3.0 or above.
Applies to
- Windows Server 2008,
- Windows Server 2008 R2,
- Windows Server 2012,
- Windows Server 2012 R2
General
Powershell Function (Basics)
Powershell .NET Namespace
Managing Modules
How to find loaded Modules?
Get-Module
How to find available modules?
Get-Module -ListAvailable
How to import a Module?
import-module`` `` activedirectory
How to get help for Modules?
Get-Help`` `` about_module
How to import All modules all together.
Get``-``Module
-ListAvailable | Import-``Module
-Verbose
How to find particular module commands ?
Get-Command`` -Module `` activedirectory
How to find Module name of the CMDLETs
get-command get-*
Getting Help
How to get Particular CMDLET help?
get-help`` ``Get-Service
get-help Get-Service -Full
get-help Get-Service -Detailed
get-help Get-Service -ShowWindow
get-help Get-Service -Online
get-help Get-Service -Examples
How to get full help of a Module (e.g. grouppolicy, activedirectory)
GCM`` -Module `` grouppolicy`` ``|`` `` Foreach`` ``{``get-help`` ``$_``.``name`` -Examples``}`` ``GCM`` -Module ``activedirectory`` ``|`` ``Foreach`` `` {``get-help`` ``$_``.``name`` -Examples``}
How to Update Powershell Help
Update-Help -Force
Powershell environment
Powershell Color Code-MSDN
Helpful !!! when we are created HTML report using Powershell.
See http://msdn.microsoft.com/en-us/library/aa358802%28v=vs.85%29.aspx
Upgrading the Powershell Version 2 to 4 on 2008 R2
How to check Powershell Path
$Pshome
How to find Powershell Alias
GAL
How to find a particular Alias
#GAL is the Alias of Get-Alias
GAL -Definition Get-Service
How to check Powershell Version?
Environment Variable to get PowerShell Version
$PSVersionTable
OR
($PSVersionTable).BuildVersion
=================================
$host.Version.Major OR $psversiontable OR Get-Host | Select-Object Version
How to change the execution Policy?
Set``-ExecutionPolicy Unrestricted
or
Set``-ExecutionPolicy Unrestricted CurrentUser
#Without Prompt
Set-ExecutionPolicy Unrestricted -force
#Recomendad seeting is Allsigned/Remotesigned
#There are Five Policies-Restricted(Default Setting),Unrestricted,Allsigned,
Remotesigned
& Bypass.
See the details about Powershell Execution Policy.
How to Test Performance of a Powershell Script?
Measure-Command { .\DCInventory.ps1 }
See the execution time of DCInventory.ps1
How to use page break like More "Switch" at cmd Prompt.
Get``-Service | Out-Host -Paging
System Management
Event Log
When & who rebooted the system? (Below simplified command is required Powershell 3 & above, "?" is used for "where")
Get-EventLog -log System | ? EventID -EQ 12 | select username,TimeGenerated
or
Get-EventLog -log System | where EventID -EQ 12 | select username,TimeGenerated
or
Get-EventLog System | ? Source -eq user32 | select MachineName,TimeGenerated,UserName,Message -First 1
How to find LastBootupTime ? (Required Powershell 3 & above)
Get-CimInstance Win32_OperatingSystem | select csname,LastBootUpTime
Server Services Inventory.
#Total Nos. of Services.
PS C:\> $all = Get-Service
PS C:\> $all.Count
135
or PS C:\> Get-Service | measure | select count
#Stopped Services
PS C:\> $all | ? status -NE 'running'
#Running Services
PS C:\> $all | ? status -NE 'stopped'
#Count All Running Services
PS C:\> $allRunning = $all | ? status -NE 'stopped'
PS C:\> $allRunning.Count
60
#Count All stopped Services
PS C:\> $allstopped = $all | ? status -NE 'running'
PS C:\> $allstopped.Count
75
Getting Computer OS & SP for multiple Computers
$computers = get-content C:\computers.txt
Get-WmiObject win32_operatingsystem -ComputerName $computers | select CSName,Caption, CSDVersion | FL
You can query multiple systems & that script is available in Microsoft Script Center.
Installed Hotfix/Update/ServicesPack Report.
Get-HotFix
or
gwmi -Query "Select * from Win32_QuickFixEngineering"
or
gcim -Query "Select * from Win32_QuickFixEngineering"
or
systeminfo | find ": KB"
or
wmic qfe
WinRM
How to enable WinRM ?
winrm qc
Directory Servcies
Verify ForestPrep, Domainprep & Rodcprep Result
Finding FSMO For Multi Domains
You no need to change anything in that Script; also AD module is not required. Powershell internals
Find attributes from bulk computers.
Function Com-Attrs {
$Computers= get-content C:\computers.txt
foreach ($Computer in $computers)
{
Get-ADComputer $Computer |
Select-Object DNSHostName,Enabled,SamAccountName
}
}
Com-Attrs | FT -AutoSize
Get & Set the RDP for multiple Computers
http://gallery.technet.microsoft.com/scriptcenter/Get-Modify-the-RDP-status-467b2e42
Get the Internet Explorer Version for multiple Computers
http://gallery.technet.microsoft.com/scriptcenter/Servers-Inventory-report-97da5709
Powershell Workflow-Scan media files into TerraByte Storage
http://gallery.technet.microsoft.com/scriptcenter/Resume-Suspend-Job-using-f955002a
WSMAN Registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WSMAN
How to check WINRM Settings.
winrm get winrm/config
How to change "MaxEnvelopeSizekb" value ?
winrm set winrm/config @{MaxEnvelopeSizekb=``"1024"``}
Remotely getting NTP Servers from PDC - Below is the Code
$computer = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().PdcRoleOwner.Name
$Reg = [Microsoft.Win RegistryKey]::OpenRemoteBaseKey(``'LocalMachine'``, $computer)
$RegKey= $Reg.OpenSubKey(``"SYSTEM\\CurrentControlSet\\Services\\W32Time\\Parameters"``)
$NTP = $Regkey.GetValue(``"NtpServer"``)
Write``-``host $NTP ``-``BackgroundColor DarkMagenta ``-``ForegroundColor Cyan
DCs Inventory
[system.directoryservices.activedirectory.domain]::GetCurrentDomain().DomainControllers | Select Name,IPAddress,OSVersion,SiteName | FT -AutoSize
Getting the Domain Functional level
[system.directoryservices.activedirectory.domain]::GetCurrentDomain().DomainMode
Getting the Forest Functional level
[system.directoryservices.activedirectory.forest]::GetCurrentForest().ForestMode
Getting the Trust
[system.directoryservices.activedirectory.domain]::GetCurrentDomain().GetTrustRelationship()
How to restore the ActiveDirectory Objects
. See the Tombstone
Get-ADObject -Filter {LastKnownparent -eq "OU=ADFS,DC=Contoso,DC=COM"} -IncludeDeletedObjects
Restore the Object
Get-ADObject -Filter {LastKnownparent -eq "OU=ADFS,DC=Contoso,DC=COM"} -IncludeDeletedObjects | Restore-ADObject -NewName bshwjt
See the deleted Objects From Active Directory Recycle BIN
##Prerequsites : . WIndows 2008 R2 DFL 2) Active Directory Recycle Bin
Get-ADObject –SearchBase “CN=Deleted Objects,DC=Contoso,DC=Com” –ldapFilter “(objectClass=*)” -includeDeletedObjects | FL *
Total Sites
[system.directoryservices.activedirectory.forest]::GetCurrentForest().Sites.Count
All Sites Name
[system.directoryservices.activedirectory.forest]::GetCurrentForest().Sites.name
DCs
[system.directoryservices.activedirectory.domain]::GetCurrentDomain().DomainControllers
Nos. of DCs
[system.directoryservices.activedirectory.domain]::GetCurrentDomain().DomainControllers.Count
Logon Information
Get-ItemProperty -Path 'HKCU:\Volatile Environment'
How to check Active directory recycle bin enabled or not ?
Get-ADOptionalFeature -F ``'name -like "Recycle Bin Feature"'
| Select-Object EnabledScopes
Active directory recycle bin is disabled ,If above command output is empty.
Get the permission of an User for an OU
ipmo activedirectory cd ad: $ACL = get-acl 'AD:\OU=cno,ou=cno,Dc=contoso,DC=com' | select -exp Access | ? IdentityReference -match 'kerbtest' $ACL
Remote Management
Installation and Configuration for Windows Remote Management
Smallest PS Script for Get the WINRM Status of Multiple Computers
Authentication for Remote Connections
Patching and Hotfixes
Get the latest patching Date
Get-HotFix | Select HotFixID,InstalledOn,InstalledBy -First 1
DNS
How to check specific DNS Reverse Lookup Zone present or not?
dnscmd ANA-DC003 /enumzones /reverse | Select-String -Pattern ' in-addr.arpa'
Networking
How to test Port?
$SOC = New-object Net.Sockets.TcpClient $SOC.Connect(" 1",636)$SOC.Connected
See Also
- What's new in PowerShell
- PowerShell Portal
- Wiki: Portal of TechNet Wiki Portals
- Active Directory Users attributes-Powershell
Another languages available
- fr-FR : PowerShell: Guide pratique
Gallery downloads