Jamie Kurtz

Promoting architectural simplicty

  Home  |   Contact  |   Syndication    |   Login
  27 Posts | 0 Stories | 54 Comments | 5 Trackbacks



Today some quick tricks finally came together for me with Windows PowerShell. So I can finally write scripts and quickly execute them from the PowerShell shell.

If you haven't yet installed PowerShell, you can find it here: http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx.

My simple goal was to be able to a) write a short script that can take parameters, and b) be able to easily run it from the shell. Here's what I did:

  1. Close any open PowerShell windows
  2. Add your script path to the Windows PATH environment variable. I'm using "C:\Users\jkurtz\Documents\WindowsPowerShell", but it can really be anywhere.
  3. Make sure you have configured PS to run scripts. This page gives the details, but basically do the following:
    1. Open up a new PowerShell window
    2. Execute this: Set-ExecutionPolicy RemoteSigned
  4. Create a simple script, named with a "ps1" extension, in the folder specified in step (2) above. I've included a sample script below
  5. Now simply enter the name of the script at the PowerShell command-line (including any required arguments) and hit Enter

If you want to try it out, but don't have a script in mind, you can use this one. It's not the most useful script in the world, but it works for this example. Just use Notepad to create a file in your script path called "members.ps1", and paste the following contents into it.

param (
	[string] $domainname = $(throw 'please specify a domain name (can use . for local machine)'),
	[string] $groupname = $(throw 'please specify a group name')

$group =[ADSI]("WinNT://" + $domainname + "/" + $groupname)
$members = @($group.psbase.Invoke("Members")) 
$members | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)} 


Then, if you've followed the 5 steps above, you can simply enter the following at the PowerShell command-line:

members -domainname DOMAIN -groupname "domain admins"

Or, leave out the -domainname and -groupname switches:

members DOMAIN "domain admins"

For a reference and guides to using PowerShell, here's some great resources:

Note that version 2.0 of PowerShell is coming soon.

posted on Saturday, January 3, 2009 2:37 PM