PowerShell commands


To check if PowerShell module for Active Directory is installed on the local machine use:
Get-Module -ListAvailable
If the ActiveDirectory module is listed you can load it by using the following command:
Import-Module ActiveDirectory
The following examples asume that you have an Active Directory Forest with multiple subdomains and that you are executing commands with an account that is member of the Enterprise Admins group.
To get a list of all domains in the Active Directory forest:
Get-ADForest | Select-Object -ExpandProperty Domains
This can be also written in a shorter form as:
(Get-ADForest).Domains
To get a count of all domains in the Active Directory forest:
Get-ADForest | Select-Object -ExpandProperty Domains | Measure-Object | Select-Object Count
This can be also written in a shorter form as:
(Get-ADForest).Domains.Count
To get a list of Active Directory Forest Operation Masters:
Get-ADForest | Select-Object DomainNamingMaster,SchemaMaster

To get the information about the Active Directory Forest Operation Mode:
Get-ADForest | Select-Object ForestMode

To get a list of all Active Directory sites:
Get-ADForest | Select-Object -ExpandProperty Sites | Sort-Object
This can be also written in a shorter form as:
(Get-ADForest).Sites | Sort-Object

To get a count of Active Directory sites:
Get-ADForest | Select-Object -ExpandProperty Sites | Measure-Object | Select-Object Count
This can be also written in a shorter form as:
(Get-ADForest).Sites.Count

To get a domain mode for all domains in the forest
$DomainList = (Get-ADForest).Domains
foreach ($Domain in $DomainList) {Get-ADDomain -Identity $Domain | Select-Object DNSRoot,DomainMode}

To get a list of domain operation masters for all domains in the forest
$DomainList = (Get-ADForest).Domains
foreach ($domain in $domainlist) {Get-ADDomain -Identity $domain | Select-Object DNSRoot,PDCEmulator,RidMaster,InfrastructureMaster | Format-List}

To get a list of all domain controllers in a forest, sorted alpahbetically with their IPv4 Addresses
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | Select-Object HostName, IPv4Address | Sort-Object Hostname

To get a count of domain controllers in the forest
((Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ }).count

To get a domain controller list, grouped by operating system
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | Select-Object Hostname,OperatingSystem | Sort-Object OperatingSystem,Hostname | Format-Table Hostname -GroupBy OperatingSystem

To get a domain controller count per operating system
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | Select-Object Hostname,OperatingSystem | Sort-Object OperatingSystem,Hostname | Group-Object OperatingSystem | Select-Object Count,Name

To get a domain controller list with operating system and service pack information
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | Select-Object Hostname,OperatingSystem,OperatingSystemServicePack | Sort-Object OperatingSystem,OperatingSystemServicePack,Hostname

To get a domain controller count per operating system and service pack
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | Select-Object Hostname,OperatingSystem,OperatingSystemServicePack | Sort-Object OperatingSystem,OperatingSystemServicePack,Hostname | Group-Object OperatingSystem,OperatingSystemServicePack | Select-Object Count,Name

To get a list of all Domain Controllers in a forest which are Global Catalogs
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter {isGlobalCatalog -eq $True} -Server $_ } | Select-Object Hostname | Sort-Object Hostname
This can be also written in a shorter form as:
(Get-ADForest).GlobalCatalogs | Sort-Object

To get a count of global catalogs
(Get-ADForest).GlobalCatalogs.Count

To get a list of all Domain Controllers in the forest that are NOT configured as Global Catalogs
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter {isGlobalCatalog -ne $True} -Server $_ } | Select-Object Hostname | Sort-Object Hostname

To get a list of all read-only domain controllers in the forest
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter {isReadOnly -eq $True} -Server $_ } | Select-Object Hostname | Sort-Object Hostname

To get a list of all Domain Controllers in the forest grouped by domain
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | Select-Object Hostname,Domain | Sort-Object Domain,Hostname | Format-Table Hostname -GroupBy Domain

To get a list of all Domain Controllers in the forest grouped by site
(Get-ADForest).Domains | %{ Get-ADDomainController -Filter * -Server $_ } | Select-Object Site,Hostname | Sort-Object Site,Hostname | Format-Table Hostname -GroupBy Site

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: