Difference between revisions of "Programming:Windows PowerShell"

From WhyAskWhy.org Wiki
Jump to: navigation, search
m (Added example of omitting column headers in the output)
m (Updated group membership example to include recursive listing and distinguished name variants)
Line 14: Line 14:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Unfortunately that doesn't expand all group members, whereas this command-line tool does:
+
If you wish to expand all group members from any included groups, run the command like so:
 +
 
 +
<syntaxhighlight lang="powershell">
 +
Import-Module ActiveDirectory
 +
Get-ADGroupMember "MyADGroupName" -recursive | Select-Object name | Sort-Object name
 +
</syntaxhighlight>
 +
 
 +
and if you wish to instead list by Distinguished Name use this:
 +
 
 +
<syntaxhighlight lang="powershell">
 +
Import-Module ActiveDirectory
 +
Get-ADGroupMember "MyADGroupName" -recursive | Select-Object distinguishedName | Sort-Object distinguishedName
 +
</syntaxhighlight>
 +
 
 +
An alternate approach is to use <code>dsquery</code> and <code>dsget</code> from the Remote Server Administration Tools (RSAT) package:
  
 
<code>
 
<code>
Line 20: Line 34:
 
</code>
 
</code>
  
 +
With the current set of options the list of users will be by Distinguished Name.
  
 
== Look up group memberships for user account ==
 
== Look up group memberships for user account ==

Revision as of 16:23, 22 April 2016


Look up Active Directory group members

I found this tip on ServerFault.com.

Import-Module ActiveDirectory
Get-ADGroupMember "MyADGroupName" | Select-Object name | Sort-Object name

If you wish to expand all group members from any included groups, run the command like so:

Import-Module ActiveDirectory
Get-ADGroupMember "MyADGroupName" -recursive | Select-Object name | Sort-Object name

and if you wish to instead list by Distinguished Name use this:

Import-Module ActiveDirectory
Get-ADGroupMember "MyADGroupName" -recursive | Select-Object distinguishedName | Sort-Object distinguishedName

An alternate approach is to use dsquery and dsget from the Remote Server Administration Tools (RSAT) package:

dsquery group -name "MyADGroupName" | dsget group -members -expand

With the current set of options the list of users will be by Distinguished Name.

Look up group memberships for user account

Import-Module ActiveDirectory
Get-ADPrincipalGroupMembership username | select name
name
----
Domain Users
Domain Computers
Workstation Admins
Company Users
Company Developers
AutomatedProcessingTeam


Leave out column headers in output

Here is an example of querying the Services list, limiting the results to 10 items and then sorting them, all as raw values:

Get-Service | Select-Object -First 10 -ExpandProperty DisplayName | sort

This gives a bare list like so:

ActiveX Installer (AxInstSV)
Adobe Acrobat Update Service
Application Experience
Application Identity
Application Information
Application Layer Gateway Service
Application Management
ASP.NET State Service
Windows Audio
Windows Audio Endpoint Builder


Links