Difference between revisions of "Programming:Windows PowerShell"

From WhyAskWhy.org Wiki
Jump to: navigation, search
m (Added example of dumping list of groups that a user account is a member of (lifted directly from stack overflow))
m (Added example of omitting column headers in the output)
Line 3: Line 3:
 
[[Category:Tips]]
 
[[Category:Tips]]
 
[[Category:NeedsCleanup]]
 
[[Category:NeedsCleanup]]
 
  
  
Line 20: Line 19:
 
dsquery group -name "MyADGroupName" | dsget group -members -expand
 
dsquery group -name "MyADGroupName" | dsget group -members -expand
 
</code>
 
</code>
 +
  
 
== Look up group memberships for user account ==
 
== Look up group memberships for user account ==
Line 37: Line 37:
 
Company Developers
 
Company Developers
 
AutomatedProcessingTeam
 
AutomatedProcessingTeam
 +
</pre>
 +
 +
 +
== 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:
 +
 +
<syntaxhighlight lang="powershell">
 +
Get-Service | Select-Object -First 10 -ExpandProperty DisplayName | sort
 +
</syntaxhighlight>
 +
 +
This gives a bare list like so:
 +
 +
<pre>
 +
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
 
</pre>
 
</pre>
  

Revision as of 10:41, 15 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

Unfortunately that doesn't expand all group members, whereas this command-line tool does:

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


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