<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7626999270234232842</id><updated>2011-07-30T18:26:19.796-07:00</updated><category term='ActiveDirectory'/><category term='Pipeline'/><category term='Get-ADUser'/><category term='[regex]'/><category term='Powershell v2'/><category term='AddDays'/><category term='Powershell'/><category term='invoke-command'/><category term='Linux Replacement'/><category term='Group-Object'/><category term='New-Grid'/><category term='on_PreviewKeyUp'/><category term='WinRM'/><category term='Where-Object (%)'/><category term='where-object'/><category term='Security'/><category term='System.Net.WebClient'/><category term='replace'/><category term='split'/><category term='Pentesting'/><category term='PSSession'/><category term='System.Text.Encoding'/><category term='Alias'/><category term='Linux'/><category term='PowershellPack'/><category term='function'/><category term='New-Label'/><category term='Get-Date'/><category term='Get-ChildControl'/><category term='Sort-Object'/><category term='PSCredential'/><category term='vbscript'/><category term='PowerShell v2 RTM'/><category term='lastlogontimestamp'/><category term='if/else'/><category term='Hash tables'/><category term='New-TextBox'/><category term='get-childitem'/><category term='WPK'/><title type='text'>PS &gt;Blog-PowerShell |  Where{$_.Blog -ge $Helpful}</title><subtitle type='html'>Dedicated to PowerShell v2 RTM running on Windows 2008 R2 or Windows 7</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>11</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-4854345112362895668</id><published>2010-02-25T09:47:00.000-08:00</published><updated>2010-03-02T07:38:52.847-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='split'/><category scheme='http://www.blogger.com/atom/ns#' term='ActiveDirectory'/><category scheme='http://www.blogger.com/atom/ns#' term='Pipeline'/><category scheme='http://www.blogger.com/atom/ns#' term='[regex]'/><category scheme='http://www.blogger.com/atom/ns#' term='PowerShell v2 RTM'/><category scheme='http://www.blogger.com/atom/ns#' term='replace'/><category scheme='http://www.blogger.com/atom/ns#' term='Powershell'/><title type='text'>The Power of -Split</title><content type='html'>I work with Active Directory everyday.  Sometimes, when I'm exporting information, I want to be able to break down the distinguishedName property to just give me the actual name.  For example, in our environment we create names in AD with LastName, FirstName.  The distguishedName for a user name like that ends up looking like this:&lt;br /&gt;&lt;br /&gt;cn=Doe\, Jane,ou=users,ou=location,dc=example,dc=com&lt;br /&gt;&lt;br /&gt;If I want to get just the name without the escape character or all the extra path info that makes up a distinguished name without PowerShell I'm probably going to copy the whole name to an editor and manually remove everthing to leave just Doe, Jane (probably just easier to type Doe, Jane instead of all of that copying stuff).  But what happens if I need to get just the LastName, FirstName of a thousand people.  Now I've some problems.  I will have to export the DN's from AD, copy them to Word or Excel and then do a bunch of replacements or string manipulation on them.&lt;br /&gt;&lt;br /&gt;Or I can write a PowerShell script to give them to me:&lt;br /&gt;&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding: 4px; text-align: left; width: 97%;"&gt;&lt;div id="codeSnippet" style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;br /&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: darkblue;"&gt;function&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;Get-NameFromDN&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: teal;"&gt;[string]&lt;/span&gt;&lt;span style="color: orangered;"&gt;$DN&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: darkblue;"&gt;return&lt;/span&gt; &lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$DN&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;replace&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: darkred;"&gt;'\'&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;&lt;span style="color: darkred;"&gt;''&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-split&lt;/span&gt; &lt;span style="color: darkred;"&gt;",*..="&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;[&lt;/span&gt;&lt;span style="color: purple;"&gt;1&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;]&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;             &lt;br /&gt;            &lt;br /&gt;&lt;span style="color: blue;"&gt;Get-QADObject&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: blue;"&gt;Get-NameFromDN&lt;/span&gt; &lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;distinguishedName&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;            &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Did you notice that I can split the string with a [regex] delimiter?&amp;nbsp; That is really cool.&amp;nbsp; Try the split yourself; paste the string below into a PowerShell prompt and hit enter:&lt;br /&gt;&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding: 4px; text-align: left; width: 97%;"&gt;&lt;div id="codeSnippet" style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;br /&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: darkred;"&gt;"cn=mygroupobjectname,ou=security groups,ou=location,dc=example,dc=com"&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-split&lt;/span&gt; &lt;span style="color: darkred;"&gt;",*..="&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;That produces:&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; margin: 20px 0px 10px; max-height: 200px; overflow: auto; padding: 4px; text-align: left; width: 97%;"&gt;&lt;div id="codeSnippet" style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;br /&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: black;"&gt;PS C:&amp;gt;&lt;/span&gt;&lt;span style="color: darkred;"&gt;"cn=mygroupobjectname,ou=security groups,ou=location,dc=example,dc=com"&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-split&lt;/span&gt; &lt;span style="color: darkred;"&gt;",*..="&lt;/span&gt;            &lt;br /&gt;            &lt;br /&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;mygroupobjectname&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;security groups&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;location&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;example&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;com&lt;/span&gt;            &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;The [regex] expression ",*..=" says find a pattern that matches zero or more commas any two characters and an equal sign.&amp;nbsp; That would match any of the following:&lt;br /&gt;cn=&lt;br /&gt;,ou=&lt;br /&gt;,dc=&lt;br /&gt;&lt;br /&gt;This pattern becomes my delimiter so that when the split function runs it will split the string where ever it finds the match.&amp;nbsp; It puts the remaining elements in a zero based array.&amp;nbsp; Because a DN starts with a pattern match the first element of the array [0] will be empty.&amp;nbsp; That is why in my Get-NameFromDN function above I retrieve element [1].&lt;br /&gt;&lt;br /&gt;PowerShell has a nice help article that explains the full functionality of -split.&amp;nbsp; I recommend that you read it to see just how powerful -split is.&amp;nbsp; You can find the help article by issuing the command:&lt;br /&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: small;"&gt;Get-Help about_split&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;I used this example to show the power of the split operation, but you might be wondering just how practical this is.&amp;nbsp; Well as it turns out I have a scheduled tasks that runs every week that does a csvde export of ActiveDirectory.&amp;nbsp; We keep the csv files around for several months just in case we delete a user and need to recreate it we have all the info we need for the recreation. &lt;br /&gt;&lt;br /&gt;When you delete a user account all of the group memberships are deleted.&amp;nbsp; So even if you use tools like ADFind and ADMod or Hyena to restore the account in AD, you will still need to get a list of groups the account belonged to from somewhere.&amp;nbsp; If you haven't backed up AD you need to query the user and they probably wont know and the whole process becomes time consuming.&amp;nbsp; That is why I do a csvde export for every domain I manage.&amp;nbsp; The csvde export creates a column in the csv file called memberof and in that column concatenates the distinguishedNames of all the non-principal groups the account belongs to with a semi-colon.&amp;nbsp; You can't just copy and paste the groups from the export into ADUC to add the groups, because, as mentioned above, ADUC's add group dialog won't accept distinguished names.&amp;nbsp; You could create a script to use the DN which would be fine.&amp;nbsp; But what if, like me, you needed to just supply the groups to your Logical Access team and they don't have any scripting ability.&amp;nbsp; Well you can run a script like this:&lt;br /&gt;&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; margin: 20px 0px 10px; max-height: 400px; overflow: auto; padding: 4px; text-align: left; width: 97%;"&gt;&lt;div id="codeSnippet" style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;br /&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: darkblue;"&gt;function&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;Get-NameFromDN&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: teal;"&gt;[string]&lt;/span&gt;&lt;span style="color: orangered;"&gt;$DN&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: darkblue;"&gt;return&lt;/span&gt; &lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$DN&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;replace&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: darkred;"&gt;'\'&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;&lt;span style="color: darkred;"&gt;''&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-split&lt;/span&gt; &lt;span style="color: darkred;"&gt;",*..="&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;[&lt;/span&gt;&lt;span style="color: purple;"&gt;1&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;]&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;             &lt;br /&gt;&lt;span style="color: orangered;"&gt;$lastname&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: darkred;"&gt;"&lt;lastname of="" user=""&gt;"&lt;/lastname&gt;&lt;/span&gt;            &lt;br /&gt;            &lt;br /&gt;&lt;span style="color: darkgreen;"&gt;&amp;lt;#The following produces a list of group DNs that are found in the csv for the users with the last name specified.  &lt;br /&gt;To narrow your search you could -match on something else like 'lastname\, firstname' or a whole different column altogether.  &lt;br /&gt;#&amp;gt;&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: blue;"&gt;Import-Csv&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;.\adexport.csv&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;?&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;DN&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-match&lt;/span&gt; &lt;span style="color: darkred;"&gt;'lastname'&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-and&lt;/span&gt; &lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;objectclass&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-eq&lt;/span&gt; &lt;span style="color: darkred;"&gt;'user'&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;memberof&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;split&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: darkred;"&gt;';'&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;            &lt;br /&gt;            &lt;br /&gt;&lt;span style="color: darkgreen;"&gt;&amp;lt;#However, you can't just copy the output, and send it to the LogicalAccess admin to paste in ADUC.  ADUC's add group &lt;br /&gt;dialog won't accept DN's.  So add the function at the end and now you get a list of names that you can copy and paste in &lt;br /&gt;an email to your LogicalAccess admin for him/her to create the account with.&lt;br /&gt;#&amp;gt;&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: blue;"&gt;Import-Csv&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;.\adexport.csv&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;?&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;DN&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-match&lt;/span&gt; &lt;span style="color: darkred;"&gt;'lastname'&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-and&lt;/span&gt; &lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;objectclass&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-eq&lt;/span&gt; &lt;span style="color: darkred;"&gt;'user'&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;memberof&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;split&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: darkred;"&gt;';'&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: blue;"&gt;Get-NameFromDN&lt;/span&gt; &lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;             &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;The first Import-Csv produces a list of group DNs.&amp;nbsp; I can't use that for ADUC.&lt;br /&gt;&lt;br /&gt;The second Import-Csv produces what I want.&amp;nbsp; Here is what it does.&lt;br /&gt;&lt;br /&gt;The second Import-Csv line passes each line of the csv file to the pipeline where a match is performed in both the DN column and the objectclass column.&amp;nbsp; Once a match is found it grabs the memberof field in the matched line and splits it based on the semi-colon as the delimiter which produces a [string[]] of distinguished names of all the groups the account belonged to.&amp;nbsp; This array is passed to the pipeline and each DN is passed to the function Get-NameFromDN which uses the powerful -split operation to split the string based on a pattern ",*..=" (which will match ou= or ,ou= or cn= or ,dc= etc...).&amp;nbsp; The function then returns the second element of the split distinguished name (remember that because a DN starts with my delimiter pattern the first element [0] is empty).&amp;nbsp; The output produces a list of Group names only, that I pasted into an email and sent to my logical access guy.&lt;br /&gt;I could have piped the group DNs to another function like Get-QADGroup $_, wrapped the function with parentheses, and gotten the name property like this:&lt;br /&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;&lt;span style="font-size: small;"&gt;...| %{(Get-QADGroup $_).name}&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;but that was significantly slower so I'd rather run it through my splitting function for speed sake.&lt;br /&gt;&lt;br /&gt;You may have noticed in my Get-NameFromDN function I used the replace method.&amp;nbsp; That is because in my environment we use Lastname, Firstname.&amp;nbsp; LDAP has to escape the comma in the DN because each path element in the DN is separated by a comma; so commas within an attribute are escaped with a slash (\).&amp;nbsp; Additionally, csvde escapes the \, again so that it is \\, in the csv file.&amp;nbsp; The replace method in the function is replacing the escape character with nothing.&amp;nbsp; That way the list that is produced can be directly used in ADUC. &lt;br /&gt;&lt;br /&gt;Of course this is just one example of how you can use the split functionality in PowerShell.&amp;nbsp; I'm sure that as you do more scripting the knowledge of the power of -split will come in handy.&lt;br /&gt;&lt;br /&gt;Have fun.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-4854345112362895668?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/4854345112362895668/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2010/02/power-of-split.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/4854345112362895668'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/4854345112362895668'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2010/02/power-of-split.html' title='The Power of -Split'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-897502018804617656</id><published>2010-02-09T11:13:00.000-08:00</published><updated>2010-02-09T16:09:30.679-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='System.Net.WebClient'/><category scheme='http://www.blogger.com/atom/ns#' term='Pentesting'/><category scheme='http://www.blogger.com/atom/ns#' term='Linux Replacement'/><category scheme='http://www.blogger.com/atom/ns#' term='Security'/><category scheme='http://www.blogger.com/atom/ns#' term='System.Text.Encoding'/><category scheme='http://www.blogger.com/atom/ns#' term='Linux'/><category scheme='http://www.blogger.com/atom/ns#' term='[regex]'/><title type='text'>Windows Pentesters Delight--Watch Out Linux Users, Move Over for MS.</title><content type='html'>Well one of my hobbies is security.&amp;nbsp; Of course anyone who wants the respect for the Security community should probably learn Linux and the tools that come with Linux.&amp;nbsp; This has led to me doing some cool stuff with Bash, learning BackTrack, and research and gaining knowledge of the Linux environment.&amp;nbsp; I've also been taking the course over at Offensive-Security.com.&amp;nbsp; I know that Linux is for serious security guru's (who would laugh at someone that said they use Windows for all their security needs), but I think that PowerShell has the ability to offer viable alternatives to some of the Linux tools.&amp;nbsp; Just throwing this out there--Linux should watch it or else it may lose it's 'Security' throne to Windows (I'm sure that comment is going to get flamed).&lt;br /&gt;&lt;br /&gt;Anyway, take for example Information Gathering Techniques.&amp;nbsp; The BackTrack distro offers a number of tools to find user info and email info about companies from web sites.&amp;nbsp; So I was thinking, "How can I start converting some of these tools over to Windows using PowerShell?"&amp;nbsp; Well here is my start...&lt;br /&gt;&lt;br /&gt;One of the first things I was thinking would be nice, would be to grab web pages to search for email addresses or other things.&amp;nbsp; So here is a quick little function for grabbing web pages:&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; margin: 20px 0px 10px; max-height: 300pt; overflow: auto; padding: 4px; text-align: left; width: 600pt;"&gt;&lt;div id="codeSnippet" style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;br /&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: darkblue;"&gt;function&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;Get-WebPage&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: darkblue;"&gt;param&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: teal;"&gt;[string]&lt;/span&gt;&lt;span style="color: orangered;"&gt;$Url&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$WebClient&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: blue;"&gt;New-Object&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;System.Net.WebClient&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$WebPage&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$WebClient&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;DownloadData&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$Url&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: darkblue;"&gt;return&lt;/span&gt; &lt;span style="color: teal;"&gt;[System.Text.Encoding]&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;::&lt;/span&gt;&lt;span style="color: black;"&gt;ASCII&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;GetString&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$WebPage&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;That little goody will get you the HTML text of the URL you supply.&amp;nbsp; "What can you do with that?" you ask.&amp;nbsp; How about getting Google results (if you get permission from Google first of course)...&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="background-color: #f4f4f4; border: 1px solid silver; cursor: text; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; margin: 20px 0px 10px; max-height: 300pt; overflow: auto; padding: 4px; text-align: left; width: 600pt;"&gt;&lt;div id="codeSnippet" style="background-color: #f4f4f4; border-style: none; color: black; direction: ltr; font-family: 'Courier New',courier,monospace; font-size: 8pt; line-height: 10pt; overflow: visible; padding: 0px; text-align: left; width: 100%;"&gt;&lt;br /&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: darkblue;"&gt;function&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;Get-GoogleResults&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: darkblue;"&gt;Param&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;  &lt;span style="color: teal;"&gt;[string]&lt;/span&gt;&lt;span style="color: orangered;"&gt;$Search&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;            &lt;br /&gt;            &lt;span style="color: teal;"&gt;[int]&lt;/span&gt;&lt;span style="color: orangered;"&gt;$PageNumber&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;=&lt;/span&gt;&lt;span style="color: purple;"&gt;1&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;            &lt;br /&gt;            &lt;span style="color: teal;"&gt;[string]&lt;/span&gt;&lt;span style="color: orangered;"&gt;$site&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;=&lt;/span&gt;&lt;span style="color: darkred;"&gt;"http://www.google.com"&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;            &lt;br /&gt;            &lt;span style="color: teal;"&gt;[string]&lt;/span&gt;&lt;span style="color: orangered;"&gt;$SearchPrefix&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: darkred;"&gt;"/search?q="&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: black;"&gt;)&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: darkblue;"&gt;Function&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;Get-ResultObject&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: teal;"&gt;[string]&lt;/span&gt;&lt;span style="color: orangered;"&gt;$rTitle&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;&lt;span style="color: teal;"&gt;[string]&lt;/span&gt;&lt;span style="color: orangered;"&gt;$rUrl&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: orangered;"&gt;$tempobject&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: blue;"&gt;New-Object&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;PSObject&lt;/span&gt; &lt;span style="color: navy;"&gt;-Property&lt;/span&gt; &lt;span style="color: black;"&gt;@{&lt;/span&gt;&lt;span style="color: black;"&gt;Title&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;=&lt;/span&gt;&lt;span style="color: orangered;"&gt;$null&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;&lt;span style="color: black;"&gt;Link&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;=&lt;/span&gt;&lt;span style="color: orangered;"&gt;$null&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: orangered;"&gt;$tempobject&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;Title&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$rTitle&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: orangered;"&gt;$tempobject&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;Link&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$rUrl&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: darkblue;"&gt;return&lt;/span&gt; &lt;span style="color: orangered;"&gt;$tempobject&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: black;"&gt;}&lt;/span&gt;                  &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$PageNumber&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$PageNumber&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-&lt;/span&gt; &lt;span style="color: purple;"&gt;1&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;*&lt;/span&gt; &lt;span style="color: purple;"&gt;10&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$WebSearch&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: darkred;"&gt;"$site$searchprefix$search`&amp;amp;start=$PageNumber"&lt;/span&gt;             &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$regex&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: teal;"&gt;[regex]&lt;/span&gt;&lt;span style="color: darkred;"&gt;'&amp;lt;h3 class=r&amp;gt;.*?&amp;lt;/h3&amp;gt;'&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$GoogleResults&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: blue;"&gt;Get-WebPage&lt;/span&gt; &lt;span style="color: orangered;"&gt;$WebSearch&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$regex&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;matches&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$GoogleResults&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt;             &lt;br /&gt;        &lt;span style="color: blue;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;  &lt;span style="color: orangered;"&gt;$Title&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: teal;"&gt;[regex]&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;::&lt;/span&gt;&lt;span style="color: black;"&gt;replace&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;&lt;span style="color: darkred;"&gt;"&amp;lt;.*?&amp;gt;"&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;&lt;span style="color: darkred;"&gt;""&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;            &lt;br /&gt;            &lt;span style="color: orangered;"&gt;$Link&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: teal;"&gt;[regex]&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;::&lt;/span&gt;&lt;span style="color: black;"&gt;matches&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;&lt;span style="color: darkred;"&gt;'\".*\"'&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;value&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;trim&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: darkred;"&gt;'`"'&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;            &lt;br /&gt;            &lt;span style="color: orangered;"&gt;$AllResults&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;+=&lt;/span&gt; &lt;span style="color: black;"&gt;@(&lt;/span&gt;&lt;span style="color: blue;"&gt;Get-ResultObject&lt;/span&gt; &lt;span style="color: orangered;"&gt;$Title&lt;/span&gt; &lt;span style="color: orangered;"&gt;$Link&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: black;"&gt;}&lt;/span&gt;                &lt;br /&gt;    &lt;span style="color: darkblue;"&gt;return&lt;/span&gt; &lt;span style="color: orangered;"&gt;$AllResults&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;The Get-GoogleResults will return the Title and link for each search result.&amp;nbsp; You can supply the page number to get the next page of results.&amp;nbsp; I.e. &lt;i&gt;Get-GoogleResults 'beer lovers' 3&lt;/i&gt; will retrieve the third page of results from a Google search.&lt;br /&gt;&lt;br /&gt;That particular function returns an array of objects with Title and Link as properties.&amp;nbsp; You can then go through the array get the URL for each link and search it if you want for email addresses.&lt;br /&gt;&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; text-align: left; line-height: 10pt; background-color: #f4f4f4; width: 600pt; font-family: 'Courier New',courier,monospace; direction: ltr; max-height: 200pt; font-size: 8pt; cursor: text;"&gt;&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 10pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;"&gt;&lt;br /&gt;&lt;pre class='PowerShellColorizedScript'&gt;&lt;span style='color:#00008b'&gt;for&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$i&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;=&lt;/span&gt;&lt;span style='color:#800080'&gt;1&lt;/span&gt;&lt;span style='color:#000000'&gt;;&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$i&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;-le&lt;/span&gt; &lt;span style='color:#800080'&gt;10&lt;/span&gt;&lt;span style='color:#000000'&gt;;&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$i&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;++&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#0000ff'&gt;Get-GoogleResults&lt;/span&gt; &lt;span style='color:#8b0000'&gt;"samueladams.com"&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$i&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;%&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#0000ff'&gt;Get-WebPage&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;Link&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;%&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#008080'&gt;[regex]&lt;/span&gt;&lt;span style='color:#8b0000'&gt;'\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*'&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;matches&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;%&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;value&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;I'm pretty sure there's a couple of Linux tools that'll replace (LOL).&lt;br /&gt;&lt;br /&gt;Have fun&lt;br /&gt;Cameron&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-897502018804617656?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/897502018804617656/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2010/02/windows-pentesters-delight.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/897502018804617656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/897502018804617656'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2010/02/windows-pentesters-delight.html' title='Windows Pentesters Delight--Watch Out Linux Users, Move Over for MS.'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-9107803276722266324</id><published>2010-01-07T15:27:00.000-08:00</published><updated>2010-01-15T15:33:24.254-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ActiveDirectory'/><category scheme='http://www.blogger.com/atom/ns#' term='Pipeline'/><category scheme='http://www.blogger.com/atom/ns#' term='Get-ADUser'/><category scheme='http://www.blogger.com/atom/ns#' term='PowerShell v2 RTM'/><category scheme='http://www.blogger.com/atom/ns#' term='Alias'/><category scheme='http://www.blogger.com/atom/ns#' term='Where-Object (%)'/><category scheme='http://www.blogger.com/atom/ns#' term='Group-Object'/><category scheme='http://www.blogger.com/atom/ns#' term='Sort-Object'/><category scheme='http://www.blogger.com/atom/ns#' term='if/else'/><title type='text'>Get-Oneliner | Group it | Sort it | Format it</title><content type='html'>Well if you read my first post on my blog you will note that I'm not (or wasn't) a fan of one-liners.&amp;nbsp; I really like structured code; and PowerShell does a great job at letting me do that.&amp;nbsp; Recently, though, I've had a change of heart (the more I use PowerShell the more I change my views on it).&amp;nbsp; I've now officially become a fan of one-liners.&lt;br /&gt;&lt;br /&gt;I've been reading Dr. Tobias Weltner's book &lt;a href="http://powershell.com/Mastering-PowerShell.pdf"&gt;Mastering-PowerShell&lt;/a&gt; (I find it to be very well written and super helpful).&amp;nbsp; Chapter 5 entitled "The PowerShell Pipeline" was very enlightening.&amp;nbsp; So I wrote a one-liner that gets all AD users and groups them by manager, sorts them and displays them in a nice text tree.&amp;nbsp; I have two versions.&amp;nbsp; I wrote the first one because I have a multi-domain forest and some of the mangers are not in the same domain as the users and currently the ActiveDirectory module won't let you search cross domain to my knowledge.&amp;nbsp; The second example is for a single domain forest.&amp;nbsp; Both one-liners take into consideration that the user object my not have the manager attribute set.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;&lt;u&gt;Example 1 Multi-domain Forest&lt;/u&gt; &lt;/b&gt;&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;i&gt;(replace&lt;b&gt; foreigndomain&lt;/b&gt; and &lt;b&gt;localdomain&lt;/b&gt; with real domain names)&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: blue;"&gt;Get-ADUser&lt;/span&gt; &lt;span style="color: navy;"&gt;-filter&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;*&lt;/span&gt; &lt;span style="color: navy;"&gt;-Properties&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;samaccountname&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;displayname&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;manager&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt;             &lt;br /&gt;    &lt;span style="color: blue;"&gt;group&lt;/span&gt; &lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkblue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;manager&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-ne&lt;/span&gt; &lt;span style="color: orangered;"&gt;$null&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-and&lt;/span&gt; &lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;manager&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-imatch&lt;/span&gt; &lt;span style="color: darkred;"&gt;"dc=localdomain"&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;Get-ADObject&lt;/span&gt; &lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;manager&lt;/span&gt; &lt;span style="color: navy;"&gt;-Properties&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;displayname&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;displayname&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: darkblue;"&gt;else&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: darkblue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;manager&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-imatch&lt;/span&gt; &lt;span style="color: darkred;"&gt;"dc=foreigndomain"&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkred;"&gt;"FOREIGNDOMAINNAME"&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: darkblue;"&gt;else&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: darkred;"&gt;"UNKNOWN"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt;             &lt;br /&gt;        &lt;span style="color: blue;"&gt;sort&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;name&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt;             &lt;br /&gt;        &lt;span style="color: blue;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt; &lt;span style="color: darkred;"&gt;"-------------------"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt; &lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;Group&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt;             &lt;br /&gt;            &lt;span style="color: blue;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkblue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;displayname&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-ne&lt;/span&gt; &lt;span style="color: orangered;"&gt;$null&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkred;"&gt;"`t$($_.displayname)"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: darkblue;"&gt;else&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkred;"&gt;"`t$($_.samaccountname)"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt; &lt;span style="color: darkred;"&gt;"`n"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 97.5%; font-family: 'Courier New',courier,monospace; direction: ltr; max-height: 500px; font-size: 10pt; cursor: text;"&gt;&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 10pt;"&gt;&lt;pre class='PowerShellColorizedScript'&gt;&lt;span style='color:#0000ff'&gt;Get-ADUser&lt;/span&gt; &lt;span style='color:#000080'&gt;-filter&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;*&lt;/span&gt; &lt;span style='color:#000080'&gt;-Properties&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;samaccountname&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;,&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;displayname&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;,&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;manager&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;group&lt;/span&gt; &lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#00008b'&gt;if&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;manager&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;-ne&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$null&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;-and&lt;/span&gt; &lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;manager&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;-imatch&lt;/span&gt; &lt;span style='color:#8b0000'&gt;"dc=localdomain"&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#0000ff'&gt;Get-ADObject&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;manager&lt;/span&gt; &lt;span style='color:#000080'&gt;-Properties&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;displayname&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;displayname&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#00008b'&gt;else&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#00008b'&gt;if&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;manager&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;-imatch&lt;/span&gt; &lt;span style='color:#8b0000'&gt;"dc=foreigndomain"&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#8b0000'&gt;"FOREIGNDOMAINNAME"&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#00008b'&gt;else&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#8b0000'&gt;"UNKNOWN"&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;sort&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;name&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;%&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;Name&lt;/span&gt;&lt;span style='color:#000000'&gt;;&lt;/span&gt; &lt;span style='color:#8b0000'&gt;"-------------------"&lt;/span&gt;&lt;span style='color:#000000'&gt;;&lt;/span&gt; &lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;Group&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;%&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#00008b'&gt;if&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;displayname&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;-ne&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$null&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#8b0000'&gt;"`t$($_.displayname)"&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#00008b'&gt;else&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#8b0000'&gt;"`t$($_.samaccountname)"&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#000000'&gt;;&lt;/span&gt; &lt;span style='color:#8b0000'&gt;"`n"&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;            &lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;&lt;u&gt;Example 2 Single-domain Forest&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: blue;"&gt;Get-ADUser&lt;/span&gt; &lt;span style="color: navy;"&gt;-filter&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;*&lt;/span&gt; &lt;span style="color: navy;"&gt;-Properties&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;samaccountname&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;displayname&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;manager&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt;             &lt;br /&gt;    &lt;span style="color: blue;"&gt;group&lt;/span&gt; &lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkblue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;manager&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-ne&lt;/span&gt; &lt;span style="color: orangered;"&gt;$null&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: blue;"&gt;Get-ADObject&lt;/span&gt; &lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;manager&lt;/span&gt; &lt;span style="color: navy;"&gt;-Properties&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;displayname&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;displayname&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: darkblue;"&gt;else&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkred;"&gt;"UNKNOWN"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: blue;"&gt;sort&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;name&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt;             &lt;br /&gt;    &lt;span style="color: blue;"&gt;%&lt;/span&gt; &lt;span style="color: black;"&gt;{&lt;/span&gt; &lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;Name&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;             &lt;br /&gt;        &lt;span style="color: darkred;"&gt;"-------------------"&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;Group&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;%&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkblue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;displayname&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-ne&lt;/span&gt; &lt;span style="color: orangered;"&gt;$null&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkred;"&gt;"`t$($_.displayname)"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: darkblue;"&gt;else&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: darkred;"&gt;"`t$($_.samaccountname)"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;;&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: darkred;"&gt;"`n"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 97.5%; font-family: 'Courier New',courier,monospace; direction: ltr; max-height: 200px; font-size: 10pt; cursor: text;"&gt;&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 10pt;"&gt;&lt;pre class='PowerShellColorizedScript'&gt;&lt;span style='color:#0000ff'&gt;Get-ADUser&lt;/span&gt; &lt;span style='color:#000080'&gt;-filter&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;*&lt;/span&gt; &lt;span style='color:#000080'&gt;-Properties&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;samaccountname&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;,&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;displayname&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;,&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;manager&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;group&lt;/span&gt; &lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#00008b'&gt;if&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;manager&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;-ne&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$null&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#0000ff'&gt;Get-ADObject&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;manager&lt;/span&gt; &lt;span style='color:#000080'&gt;-Properties&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;displayname&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;displayname&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#00008b'&gt;else&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#8b0000'&gt;"UNKNOWN"&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;sort&lt;/span&gt; &lt;span style='color:#8a2be2'&gt;name&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;%&lt;/span&gt; &lt;span style='color:#000000'&gt;{&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;Name&lt;/span&gt;&lt;span style='color:#000000'&gt;;&lt;/span&gt;&lt;span style='color:#8b0000'&gt;"-------------------"&lt;/span&gt;&lt;span style='color:#000000'&gt;;&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;Group&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;|&lt;/span&gt; &lt;span style='color:#0000ff'&gt;%&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#00008b'&gt;if&lt;/span&gt;&lt;span style='color:#000000'&gt;(&lt;/span&gt;&lt;span style='color:#ff4500'&gt;$_&lt;/span&gt;&lt;span style='color:#a9a9a9'&gt;.&lt;/span&gt;&lt;span style='color:#000000'&gt;displayname&lt;/span&gt; &lt;span style='color:#a9a9a9'&gt;-ne&lt;/span&gt; &lt;span style='color:#ff4500'&gt;$null&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#8b0000'&gt;"`t$($_.displayname)"&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#00008b'&gt;else&lt;/span&gt;&lt;span style='color:#000000'&gt;{&lt;/span&gt;&lt;span style='color:#8b0000'&gt;"`t$($_.samaccountname)"&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;span style='color:#000000'&gt;)&lt;/span&gt;&lt;span style='color:#000000'&gt;;&lt;/span&gt;&lt;span style='color:#8b0000'&gt;"`n"&lt;/span&gt;&lt;span style='color:#000000'&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Output will be something like this:&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 97.5%; font-family: 'Courier New',courier,monospace; direction: ltr; max-height: 600px; font-size: 10pt; cursor: text;"&gt;&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 10pt;"&gt;&lt;pre&gt;&lt;span&gt;&lt;br /&gt;PS C:\&gt;Get-ADUser -filter * -Properties samaccountname, displayname, manager | group {if(($_.manager -ne $null) -and ($_.manager -imatch "dc=localdomain")){(Get-ADObject $_.manager -Properties displayname).displayname}else{if($_.manager -imatch "dc=foreigndomain"){"FOREIGNDOMAINNAME"}else{"UNKNOWN"}}} | sort name | %{$_.Name; "-------------------"; ($_.Group | %{if($_.displayname -ne $null){"`t$($_.displayname)"}else{"`t$($_.samaccountname)"}}); "`n"}            &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Foreign Domain&lt;br /&gt;--------------------&lt;br /&gt;    Employee4&lt;br /&gt;    Employee5&lt;br /&gt;    Employee6&lt;br /&gt;    Employee7&lt;br /&gt;&lt;br /&gt;Manager1&lt;br /&gt;--------------------&lt;br /&gt;    Employee1&lt;br /&gt;    Employee2&lt;br /&gt;&lt;br /&gt;Manager2&lt;br /&gt;--------------------&lt;br /&gt;    Employee3&lt;br /&gt;&lt;br /&gt;UNKNOWN&lt;br /&gt;--------------------&lt;br /&gt;    Employee8&lt;br /&gt;    Employee9&lt;br /&gt;    .&lt;br /&gt;    .&lt;br /&gt;    .&lt;br /&gt;    etc...&lt;br /&gt;&lt;br /&gt;You get the idea.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;Have fun.!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-9107803276722266324?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/9107803276722266324/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2010/01/get-oneliner-group-thought-sort-it-out.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/9107803276722266324'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/9107803276722266324'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2010/01/get-oneliner-group-thought-sort-it-out.html' title='Get-Oneliner | Group it | Sort it | Format it'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-2509041840632000037</id><published>2010-01-04T09:49:00.000-08:00</published><updated>2010-01-04T10:08:45.690-08:00</updated><title type='text'>I definately recommend the PowerShellPack.</title><content type='html'>When I first started developing for PowerShell I was using the PowerShell ISE that comes with Windows 7.&amp;nbsp; Mainly because my version of Sapien PrimalScript doesn't support PowerShell v2 with Intellisense.&amp;nbsp; So I looked all over for a snazzy IDE that gave me full support for PS 2.&amp;nbsp;  I liked &lt;a href="http://www.powergui.org/index.jspa"&gt;Quest's editor (PowerGUI)&lt;/a&gt; the best, but it just doesn't work at present with the AD module from Microsoft (go figure--Quest has their own AD tools, which are very robust).&amp;nbsp; So I checked out Idera's PowerShellPlus.&amp;nbsp;  It is just a bit much for me.&amp;nbsp; Don't get me wrong.&amp;nbsp; PowerShellPlus is really amazing and&lt;a href="http://powershell.com/cs/blogs/ebook/"&gt; Dr. Tobias Weltner has a really good book&lt;/a&gt; (and it's free) to help learn PowerShell, but the IDE is just too much for me (price wise-at least for now).&amp;nbsp; Also, Sapien's &lt;a href="http://www.primaltools.com/products/info.asp?p=PrimalScript"&gt;PrimalScript 2009&lt;/a&gt; is supposed to be very good (I've been using 2007 for all my VBScripting for years now and I really like their IDE).&lt;br /&gt;&lt;br /&gt;However, the more I used PowerShell ISE the more I enjoyed developing with it.  It has a great interactive environment.&amp;nbsp; I can try out my commands first in the console window then go on the left pane (where I've located the editor in my environment), and type my code when it finally works.  It's so simple, yet so powerful.  &lt;br /&gt;&lt;br /&gt;But...I want intellisense (so I thought).&amp;nbsp; Also I'm blogging now, and I want to copy my nice color coded script to my blog.&amp;nbsp;  Well there really isn't a need for intellisense in ISE.  Microsft allows the script editor to use tab completion (out-of-the-box).&amp;nbsp;  I love tab completion and behaves just like the PS console.&amp;nbsp; Once you get used to tab completion intellisense becomes annoying (at least in my case it did).&lt;br /&gt;&lt;br /&gt;What about my blogging though.  Well that is where the PowerShellPack comes in (finally--maybe I should have titled this blog entry something else).&amp;nbsp;  When you install the pack (get it &lt;a href="http://code.msdn.microsoft.com/PowerShellPack/Release/ProjectReleases.aspx?ReleaseId=3341"&gt;&lt;u&gt;here&lt;/u&gt;&lt;/a&gt;) it adds a ton of features to the ISE.&amp;nbsp; I can now copy my code in colored HTML straight to the clipboard so that I can post it in my blog.&amp;nbsp; Isn't that great!&lt;br /&gt;&lt;br /&gt;That's not all the PowerShellPack does.&amp;nbsp; It comes with 10 modules that let you do all kinds of things.&amp;nbsp; I'm playing around with building UIs using the new cmdlets around WPF.&amp;nbsp; Good stuff.&amp;nbsp; Head over to the &lt;a href="http://code.msdn.microsoft.com/PowerShellPack"&gt;main site&lt;/a&gt;.&amp;nbsp; Check out some videos and have fun.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-2509041840632000037?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/2509041840632000037/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2010/01/i-definately-recommend-powershellpack.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/2509041840632000037'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/2509041840632000037'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2010/01/i-definately-recommend-powershellpack.html' title='I definately recommend the PowerShellPack.'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-3374438357577200655</id><published>2010-01-04T09:29:00.000-08:00</published><updated>2010-01-04T09:29:56.982-08:00</updated><title type='text'>Running PowerShell v2 RTM on Something Other Than Win 7 or Win 2008R2</title><content type='html'>I know this site is dedicated to running PowerShell v2 RTM on Windows 7 or Windows 2008 R2.&amp;nbsp; However if you would like to start playing with PowerShell v2 RTM on some other operating system go &lt;a href="http://support.microsoft.com/kb/968929"&gt;&lt;u&gt;here&lt;/u&gt;&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Have fun.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-3374438357577200655?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/3374438357577200655/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2010/01/running-powershell-v2-rtm-on-something.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/3374438357577200655'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/3374438357577200655'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2010/01/running-powershell-v2-rtm-on-something.html' title='Running PowerShell v2 RTM on Something Other Than Win 7 or Win 2008R2'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-5279319437737673542</id><published>2010-01-04T09:23:00.000-08:00</published><updated>2010-01-11T11:09:06.304-08:00</updated><title type='text'>How to use PowerShell v2 RTM and the ActiveDirectory Module that comes with RSAT.</title><content type='html'>I noticed that not a lot of people are using the new Active Directory module in PowerShell v2 RTM (ps2rtm).&amp;nbsp; &lt;i&gt;Maybe it's because you can only take advantage of the AD module if you are running Windows 7.&amp;nbsp; Windows 7 with AD module and ADWS will let you manage Win 2003 AD but you cannot run the PowerShell AD module on Win XP or 2003.&amp;nbsp; &lt;/i&gt;&lt;br /&gt;&lt;br /&gt;I had been avoiding PowerShell until recently for the more familiar (in my case) vbscript for several years now.&amp;nbsp; I just didn't want to give up vb.&amp;nbsp; [adsi] in PowerShell didn't seem that much easier than adsi in vbscript.&amp;nbsp; However, recently a catastrophic event took place in my life (all my work in vb for the last year were gone) which caused me to re-evaluate PowerShell.&amp;nbsp; I'd been putting it off but now forces were at work that kind of shoved me in that direction.&amp;nbsp; So I dove in.&amp;nbsp; I have to say--I am so glad I did.&lt;br /&gt;&lt;br /&gt;That said I'm finding that a large majority of the posh community are not using the latest greatest.&amp;nbsp; I can understand that.&amp;nbsp; Had I invested a couple of years in ps1 I would be reluctant to move to ps2rtm for production stuff (look at me I didn't even want to move away from vbscript).&amp;nbsp; Plus I was reluctant to move to Windows 7.&amp;nbsp; &lt;i&gt;All my colleagues were using Win7 RCx not sure which version.&amp;nbsp; I waited until RTM and then only installed it on an extra workstation in my office.&amp;nbsp; Now I run it on my primary workstation--it really is a nice OS.&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Anyway, I just wanted to say to moving to ps2rtm and using the new ActiveDirectory module is not that painful (if you are running Windows 7).&amp;nbsp; It takes all of 15 minutes.&amp;nbsp; I'm not trying to diminish the value of Quest tools, but really the future of ps2 includes a fully functional, fully featured set of AD tools.&amp;nbsp; And it's really not that hard to start taking advantage of them (even if you are running a Windows 2003 functional AD environment).&lt;br /&gt;&lt;br /&gt;First prepare your AD environment.&amp;nbsp; &lt;a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=008940c6-0296-4597-be3e-1d24c1cf0dda"&gt;&lt;u&gt;Here&lt;/u&gt;&lt;/a&gt; are the instructions and download.&amp;nbsp; Read the page carefully.&amp;nbsp; For Windows 2003 DCs you will need to install two patches (the links for the patches are in the instructions on that page) and you may need to install the latest .Net 3.5 from Microsoft Update site.&amp;nbsp; Again all of the instructions and prerequisites are found on the page I linked to above.&amp;nbsp; I'm running a Windows 2003 native AD environment on DCs with Windows 2003 SP2 (no R2 in my environment) and ADWS works great.&amp;nbsp; You only need one DC running ADWS in each domain.&lt;br /&gt;&lt;br /&gt;Next install the RSAT tools in Windows 7.&amp;nbsp; Installation and instructions are &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7D2F6AD7-656B-4313-A005-4E344E43997D&amp;amp;displaylang=en"&gt;&lt;u&gt;here&lt;/u&gt;&lt;/a&gt;.&amp;nbsp; After installing the RSAT tools you need to enable the AD features.&lt;br /&gt;&lt;br /&gt;If you have prepared your environment according to the instructions, then, after starting PowerShell enter the command &lt;i&gt;import-module activedirectory&lt;/i&gt;.&amp;nbsp; &lt;br /&gt;&lt;br /&gt;enter &lt;i&gt;get-help *-ad*&lt;/i&gt; or &lt;i&gt;get-command -Module ActiveDirectory&lt;/i&gt; to see all of the AD functionality that the ActiveDirectory module gives.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;Sorry Windows XP users or Windows 2003 server users.&amp;nbsp; The ActiveDirectory module is not (as of yet) available for those systems.&amp;nbsp; I saw a number of forums with complaints about that but with no response so not sure if MS will ever make the module available for those systems.&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Have fun.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-5279319437737673542?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/5279319437737673542/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2010/01/how-to-use-powershell-v2-rtm-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/5279319437737673542'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/5279319437737673542'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2010/01/how-to-use-powershell-v2-rtm-and.html' title='How to use PowerShell v2 RTM and the ActiveDirectory Module that comes with RSAT.'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-2508720631483112366</id><published>2009-12-30T11:37:00.000-08:00</published><updated>2010-01-15T15:08:23.351-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='PowershellPack'/><category scheme='http://www.blogger.com/atom/ns#' term='New-Label'/><category scheme='http://www.blogger.com/atom/ns#' term='New-Grid'/><category scheme='http://www.blogger.com/atom/ns#' term='Get-ChildControl'/><category scheme='http://www.blogger.com/atom/ns#' term='WPK'/><category scheme='http://www.blogger.com/atom/ns#' term='PowerShell v2 RTM'/><category scheme='http://www.blogger.com/atom/ns#' term='New-TextBox'/><category scheme='http://www.blogger.com/atom/ns#' term='on_PreviewKeyUp'/><title type='text'>WPK--Tough to learn but very rewarding.</title><content type='html'>&lt;span style="font-size: small;"&gt;Well I've been trying to figure out how to build an app with WPK (Windodws Presentation Framework PowerShell Kit).&amp;nbsp; James Brundage has some really nice videos you can check out &lt;a href="http://code.msdn.microsoft.com/PowerShellPack/Wiki/View.aspx?title=Videos&amp;amp;referringTitle=Home"&gt;here&lt;/a&gt;.&amp;nbsp; The main site for WPK is &lt;a href="http://code.msdn.microsoft.com/PowerShellPack"&gt;here&lt;/a&gt; (which is really the home site for the PowerShellPack).&amp;nbsp; WPK is just one module that comes in the PowerShellPack.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;The reason I say "Tough to learn..." is due to the general lack of good documentation.&amp;nbsp; For example if I get-help new-textbox I get all of the different parameters all jumbled up together.&amp;nbsp; If I use the -full switch on get-help it displays all of the parameters individually, but there is no explanation on how to use the parameters or what kind of data to feed the parameters (the jumbled parameters in some cases show what type of data the parameter is looking for).&amp;nbsp; So within PowerShell's help system (outside of finding parameters) it becomes difficult figuring out how to use the parameters.&amp;nbsp; For example one parameter on almost all WPK new-* cmdlets is the -DataBinding parameter, but there is nothing within PowerShell that explains how to use that parameter.&amp;nbsp; If you look online, the data you feed that parameter is very complex.&amp;nbsp; So then you go to the WPF MS site and try to interpret C# into PowerShell because none of the examples on the WPF site show how to use PowerShell cmdlets.&amp;nbsp; For me it turned into a guess fest.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;So, yes, tough to learn.&amp;nbsp; However, after ton's of digging today, I found very rewarding what I can actually do with WPK once you know how it works.&amp;nbsp; It'll be nice to see some good documentation in one place in the future.&amp;nbsp; If someone is aware of a such a place please let me know.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: small;"&gt;&lt;br /&gt;Anyway, I started playing around with it today and had problems referencing other objects inside the main window.&amp;nbsp; For example if I wanted to change the content of a label with the text from a TextBox, I was finding it difficult to do.&amp;nbsp; I found a bunch of information on -DataBinding (which I have to say was at a minimum confusing).&amp;nbsp; Again, there is so little documentation on WPF as it relates to PowerShell cmdlets.&amp;nbsp; I spent hours trying to make the text on one TextBox equal to the text in another Textbox.&amp;nbsp; Well it ends up that the best place to find answers are the examples that get installed when you install the PowerShellPack.&amp;nbsp; I found the examples on my %systemdirve%\Users\%username%\My Documents\WindowsPowerShell\Modules\WPK\Examples.&lt;br /&gt;&lt;br /&gt;Here is a quick script that sort of emulates a chat window.&lt;/span&gt;&lt;span style="font-size: small;"&gt;&lt;span id="ctl00_Content_UserNameHeaderLabel"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 97.5%; font-family: 'Courier New',courier,monospace; direction: ltr; max-height: 200px; font-size: 10pt; cursor: text;"&gt;&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 10pt;"&gt;&lt;pre class="PowerShellColorizedScript"&gt;&lt;span style="color: blue;"&gt;New-Grid&lt;/span&gt; &lt;span style="color: navy;"&gt;-Name&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;gGrid1&lt;/span&gt; &lt;span style="color: navy;"&gt;-Background&lt;/span&gt;  &lt;span style="color: blueviolet;"&gt;blue&lt;/span&gt; &lt;span style="color: navy;"&gt;-Rows&lt;/span&gt; &lt;span style="color: purple;"&gt;2&lt;/span&gt; &lt;span style="color: navy;"&gt;-Columns&lt;/span&gt; &lt;span style="color: darkred;"&gt;"auto"&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;,&lt;/span&gt;&lt;span style="color: darkred;"&gt;"1*"&lt;/span&gt; &lt;span style="color: navy;"&gt;-On_Loaded&lt;/span&gt; &lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$script:AccData&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$window&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;Get-ChildControl&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;AccData&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$script:Data&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$window&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;|&lt;/span&gt; &lt;span style="color: blue;"&gt;Get-ChildControl&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;Data&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$AccData&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;background&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: darkred;"&gt;"black"&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: orangered;"&gt;$AccData&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;foreground&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: darkred;"&gt;"lime"&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt; &lt;span style="color: navy;"&gt;-Children&lt;/span&gt; &lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: blue;"&gt;New-Label&lt;/span&gt; &lt;span style="color: darkred;"&gt;"Data"&lt;/span&gt; &lt;span style="color: navy;"&gt;-Row&lt;/span&gt; &lt;span style="color: purple;"&gt;0&lt;/span&gt; &lt;span style="color: navy;"&gt;-Column&lt;/span&gt; &lt;span style="color: purple;"&gt;0&lt;/span&gt; &lt;span style="color: navy;"&gt;-Foreground&lt;/span&gt; &lt;span style="color: darkred;"&gt;"yellow"&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: blue;"&gt;New-TextBox&lt;/span&gt; &lt;span style="color: navy;"&gt;-TextWrapping&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;Wrap&lt;/span&gt; &lt;span style="color: navy;"&gt;-AcceptsReturn&lt;/span&gt; &lt;span style="color: navy;"&gt;-Row&lt;/span&gt; &lt;span style="color: purple;"&gt;0&lt;/span&gt; &lt;span style="color: navy;"&gt;-Column&lt;/span&gt; &lt;span style="color: purple;"&gt;1&lt;/span&gt; &lt;span style="color: navy;"&gt;-Name&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;Data&lt;/span&gt; &lt;span style="color: navy;"&gt;-On_PreviewKeyUp&lt;/span&gt; &lt;span style="color: black;"&gt;{&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: darkblue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: orangered;"&gt;$_&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;key&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;-eq&lt;/span&gt; &lt;span style="color: darkred;"&gt;"Return"&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;                      &lt;br /&gt;            &lt;span style="color: orangered;"&gt;$AccData&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;text&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: orangered;"&gt;$AccData&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;text&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;+&lt;/span&gt; &lt;span style="color: orangered;"&gt;$this&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;text&lt;/span&gt;            &lt;br /&gt;            &lt;span style="color: orangered;"&gt;$this&lt;/span&gt;&lt;span style="color: darkgrey;"&gt;.&lt;/span&gt;&lt;span style="color: black;"&gt;text&lt;/span&gt; &lt;span style="color: darkgrey;"&gt;=&lt;/span&gt; &lt;span style="color: darkred;"&gt;""&lt;/span&gt;            &lt;br /&gt;        &lt;span style="color: black;"&gt;}&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: black;"&gt;}&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: blue;"&gt;New-Label&lt;/span&gt; &lt;span style="color: darkred;"&gt;"Accumulated Data"&lt;/span&gt; &lt;span style="color: navy;"&gt;-Row&lt;/span&gt; &lt;span style="color: purple;"&gt;1&lt;/span&gt; &lt;span style="color: navy;"&gt;-Column&lt;/span&gt; &lt;span style="color: purple;"&gt;0&lt;/span&gt; &lt;span style="color: navy;"&gt;-Foreground&lt;/span&gt; &lt;span style="color: darkred;"&gt;"yellow"&lt;/span&gt;            &lt;br /&gt;    &lt;span style="color: blue;"&gt;New-TextBox&lt;/span&gt; &lt;span style="color: navy;"&gt;-IsReadOnly&lt;/span&gt; &lt;span style="color: navy;"&gt;-TextWrapping&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;Wrap&lt;/span&gt; &lt;span style="color: navy;"&gt;-Row&lt;/span&gt; &lt;span style="color: purple;"&gt;1&lt;/span&gt; &lt;span style="color: navy;"&gt;-Column&lt;/span&gt; &lt;span style="color: purple;"&gt;1&lt;/span&gt; &lt;span style="color: navy;"&gt;-Name&lt;/span&gt; &lt;span style="color: blueviolet;"&gt;AccData&lt;/span&gt;            &lt;br /&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt; &lt;span style="color: navy;"&gt;-asjob&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;Of course I'm running PowerShell v2.0 RTM on Windows 7.&amp;nbsp; I had to install the PowerShellPack for this to work.&lt;br /&gt;&lt;br /&gt;Have fun.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-2508720631483112366?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/2508720631483112366/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/wpk-tough-to-learn-but-very-rewarding.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/2508720631483112366'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/2508720631483112366'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/wpk-tough-to-learn-but-very-rewarding.html' title='WPK--Tough to learn but very rewarding.'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-88946440731028293</id><published>2009-12-17T20:24:00.000-08:00</published><updated>2010-01-15T15:03:07.982-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Powershell v2'/><category scheme='http://www.blogger.com/atom/ns#' term='invoke-command'/><category scheme='http://www.blogger.com/atom/ns#' term='PSSession'/><category scheme='http://www.blogger.com/atom/ns#' term='WinRM'/><category scheme='http://www.blogger.com/atom/ns#' term='ActiveDirectory'/><category scheme='http://www.blogger.com/atom/ns#' term='Hash tables'/><category scheme='http://www.blogger.com/atom/ns#' term='PSCredential'/><title type='text'>Remove a user in domainA from a group in domainB</title><content type='html'>I'm writing a script in Posh (that seems to be the popular acronym for PowerShell--so I'll go with it as well) that finds all of the inactive users in the domain and moves them to an OU to be disabled one month later if the manager of the user doesn't respond to the email that the script sends.&amp;nbsp; I finished that part and and now I'm at the point that I disable user accounts where there was no response.&lt;br /&gt;&lt;br /&gt;The moving and disabling the users is rather straight forward in Posh. The problem I encounterd was removing the group memberships of the users (a business requirement when we disable the user account).&amp;nbsp; Now removing the group memberships of groups in the same domain as the user account is also very straight forward, but what happens when you try to remove the user account from a group that is located in a sibling domain in the same forest.&lt;br /&gt;Well here's what happens:&lt;br /&gt;&lt;br /&gt;&lt;div style="color: red;"&gt;Remove-ADGroupMember : Cannot find an object with identity: 'CN=Smith\, John,OU=Users,OU=location,DC=DomainA,DC=example,DC=com' under: 'DC=DomainB,DC=example,DC=com'.&lt;br /&gt;At line:1 char:21&lt;br /&gt;+ Remove-ADGroupMember &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;nbsp; -Identity $group.ObjectGUID -Members $group.member[12] -Credential $myAdminCreds.DomainB -server DomainB.example.com&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; + CategoryInfo&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;span style="text-decoration: underline;"&gt;&lt;b&gt;ObjectNotFound: (CN=Smith\, Joh...,DC=example,DC=com:ADPrincipal) &lt;/b&gt; &lt;/span&gt; [Remove-ADGroupMember], ADIdentityNotFoundException&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.RemoveADGroupMember&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&amp;nbsp;All in nice bright red wording.&lt;br /&gt;&lt;br /&gt;Here is a script similar to the one that generated that error:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: cadetblue; font-weight: bold;"&gt;Import-Module&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: maroon;"&gt;ActiveDirectory&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: purple;"&gt;$adminCreds&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; @{domainA &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: cadetblue; font-weight: bold;"&gt;Get-Credential&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: maroon;"&gt;domA\admin&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;;domainB &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: cadetblue; font-weight: bold;"&gt;Get-Credential&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: maroon;"&gt;domB\admin&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: purple;"&gt;$user&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; Get-ADUser johndoe -Properties memberof -Credential &lt;/span&gt;&lt;span style="color: purple;"&gt;$adminCreds&lt;/span&gt;&lt;span style="color: black;"&gt;.domainA&lt;br /&gt;&lt;/span&gt;&lt;span style="color: purple;"&gt;$groups&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: purple;"&gt;$user&lt;/span&gt;&lt;span style="color: black;"&gt;.memberof | &lt;/span&gt;&lt;span style="color: cadetblue; font-weight: bold;"&gt;where&lt;/span&gt;&lt;span style="color: black;"&gt;{&lt;/span&gt;&lt;span style="color: purple;"&gt;$_&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;-match&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: maroon;"&gt;dc=domainB&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;foreach&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: purple;"&gt;$group&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: blue;"&gt;in&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: purple;"&gt;$groups&lt;/span&gt;&lt;span style="color: black;"&gt;){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Remove-ADGroupMember -Identity &lt;/span&gt;&lt;span style="color: purple;"&gt;$group&lt;/span&gt;&lt;span style="color: black;"&gt; -Members &lt;/span&gt;&lt;span style="color: purple;"&gt;$user&lt;/span&gt;&lt;span style="color: black;"&gt;.distinguishedName -Server domainB.example.com -Credential &lt;/span&gt;&lt;span style="color: purple;"&gt;$adminCreds&lt;/span&gt;&lt;span style="color: black;"&gt;.domainB&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;Had I chosen groups from domainA, the script would have run without error.&lt;br /&gt;&lt;br /&gt;So I posted my problem on every forum I could find including 'The Official Scripting Guys Forum!' as well on #powershell irc chat on freenode.net.&amp;nbsp; To date no one has responded.&amp;nbsp; So I delved into a work-a-round.&lt;br /&gt;&lt;br /&gt;At first I tried straight up ADSI:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: purple;"&gt;$group&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; [&lt;/span&gt;&lt;span style="color: teal;"&gt;adsi&lt;/span&gt;&lt;span style="color: black;"&gt;]&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: maroon;"&gt;LDAP://$($group.distinguishedName)&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: purple;"&gt;$group&lt;/span&gt;&lt;span style="color: black;"&gt;.putEx(&lt;/span&gt;&lt;span style="color: black;"&gt;4&lt;/span&gt;&lt;span style="color: black;"&gt;,&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: maroon;"&gt;member&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt;,@(&lt;/span&gt;&lt;span style="color: purple;"&gt;$user&lt;/span&gt;&lt;span style="color: black;"&gt;.distinguishedName))&lt;br /&gt;&lt;/span&gt;&lt;span style="color: purple;"&gt;$group&lt;/span&gt;&lt;span style="color: black;"&gt;.setinfo()&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;However, because my account is not an administrator in the sibling domain I got an access denied error.&amp;nbsp; I did a bunch of research again and low-and-behold the best I got was that there is no specific way to provide credentials to a foreign domain with [adsi] (albeit a sibling domain in the same forest).&amp;nbsp; I even mapped a drive to the domain controller in domainB using my admin credentials in that domain--nadda!&lt;br /&gt;&lt;br /&gt;So after a ton of unfruitful research I developed my own work-around.  If one of you Posh gurus wants to post an easier way I'd be delighted.&amp;nbsp; I use WinRM 2.0 to accomplish this (I read a ton of help pages):&lt;br /&gt;&lt;br /&gt;&lt;div id="codeSnippetWrapper" style="border: 1px solid silver; margin: 20px 0px 10px; padding: 4px; overflow: auto; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 97.5%; font-family: 'Courier New',courier,monospace; direction: ltr; max-height: 500px; font-size: 10pt; cursor: text;"&gt;&lt;br /&gt;&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 10pt;"&gt;&lt;pre&gt;&lt;!--&lt;br /&gt;&lt;br /&gt;Code highlighting produced by Actipro SyntaxEditor&lt;br /&gt;http://www.ActiproSoftware.com/Products/DotNet/&lt;br /&gt;&lt;br /&gt;--&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Import-Module&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;ActiveDirectory&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$adminCreds&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; @{domainA &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Get-Credential&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;domA\admin&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;;domainB &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Get-Credential&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;domB\admin&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Get the user object&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$user&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Get-ADUser johndoe -Properties memberof -Credential &lt;/span&gt;&lt;span style="color: #800080;"&gt;$adminCreds&lt;/span&gt;&lt;span style="color: #000000;"&gt;.domainA&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Find groups with distinguishedNames that are in domainB&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$groups&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$user&lt;/span&gt;&lt;span style="color: #000000;"&gt;.memberof | &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt;{&lt;/span&gt;&lt;span style="color: #800080;"&gt;$_&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;-match&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;dc=domainB&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Create a remote session on domainB's domain controller&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$s&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;New-PSSession&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-ComputerName&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;domb-dc1&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-Credential&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$adminCreds&lt;/span&gt;&lt;span style="color: #000000;"&gt;.domainB&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;&amp;lt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Unfortunately you can't just reference variables in your local ps session while in the remote session so some setup &lt;br /&gt;work is needed.  Cycling through the collection of group DNs you first create a hash table (I love hash tables) with &lt;br /&gt;the parameters you will need to work with in the remote session.&lt;/span&gt;&lt;span style="color: #008000;"&gt;#&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$groups&lt;/span&gt;&lt;span style="color: #000000;"&gt;){&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$LHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; @{userDN &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$user&lt;/span&gt;&lt;span style="color: #000000;"&gt;.distinguishedName;groupDN &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;} &lt;/span&gt;&lt;span style="color: #008000;"&gt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Local Host parameters.&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;&amp;lt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;We need to send those parameters to the remote session.  The variable '$multivars' is an arbitrary name. &lt;br /&gt;    You can use what ever you makes sense in that spot. 'RHparams' Remote Host parameters will be set in the remote pssession.&lt;br /&gt;    $multivars becomes $LHparams and can be used to set a variable on the remote host.  &lt;br /&gt;    &lt;br /&gt;    You can send multiple arguments and must have multiple parameters (param($a, $b))to hold those arguments.&lt;br /&gt;    So this would work:&lt;/span&gt;&lt;span style="color: #008000;"&gt;#&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Invoke-Command&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-Session&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$s&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-ScriptBlock&lt;/span&gt;&lt;span style="color: #000000;"&gt; {&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;param&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$rhuserDN&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800080;"&gt;$rhgroupDN&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #800080;"&gt;$userDN&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$rhuserDN&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$groupDN&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$rhgroupDN&lt;/span&gt;&lt;span style="color: #000000;"&gt;}` &lt;br /&gt;                                                            -ArgumentList &lt;/span&gt;&lt;span style="color: #800080;"&gt;$user&lt;/span&gt;&lt;span style="color: #000000;"&gt;.distinguishedName,&lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;However, I like hash tables better-well actually I love hash tables (best thing since sliced bread). so:&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Invoke-Command&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-Session&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$s&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-ScriptBlock&lt;/span&gt;&lt;span style="color: #000000;"&gt; {&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;param&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$multivars&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #800080;"&gt;$RHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$multivars&lt;/span&gt;&lt;span style="color: #000000;"&gt;} -ArgumentList &lt;/span&gt;&lt;span style="color: #800080;"&gt;$LHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;&amp;lt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Now you may be tempted to enter the session in your script--but don't.  My previous post had the script doing that but it&lt;br /&gt;    doesn't work.  The commands issued in the script after entering a PSSession don't do anything.  So do this &lt;br /&gt;    (check example 5 of Help invoke-command -examples):   &lt;/span&gt;&lt;span style="color: #008000;"&gt;#&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$command&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; [&lt;/span&gt;&lt;span style="color: #008080;"&gt;adsi&lt;/span&gt;&lt;span style="color: #000000;"&gt;]&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;LDAP://$($RHparams.groupDN)&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;.putEx(&lt;/span&gt;&lt;span style="color: #000000;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;member&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;,@(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$RHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt;.userDN))&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;.setinfo()&lt;br /&gt;    }&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;&amp;lt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Note that when using the hash table in the remote session the porperties ARE case sensitive.  &lt;br /&gt;    GroupDN gets you nothing groupDN has the data.&lt;/span&gt;&lt;span style="color: #008000;"&gt;#&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;&amp;lt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Of course we want as few commands as possible so let's not run the invoke-command twice&lt;br /&gt;    instead let's do this:  &lt;/span&gt;&lt;span style="color: #008000;"&gt;#&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$LHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; @{userDN &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$user&lt;/span&gt;&lt;span style="color: #000000;"&gt;.distinguishedName;groupDN &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;}&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$command&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;param&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$multivars&lt;/span&gt;&lt;span style="color: #000000;"&gt;)&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$RHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$multivars&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; [&lt;/span&gt;&lt;span style="color: #008080;"&gt;adsi&lt;/span&gt;&lt;span style="color: #000000;"&gt;]&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;LDAP://$($RHparams.groupDN)&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;.putEx(&lt;/span&gt;&lt;span style="color: #000000;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;member&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;,@(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$RHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt;.userDN))&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;.setinfo()&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #008000;"&gt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Now send the commands to the remote session just once.&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Invoke-Command&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-Session&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$s&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-ScriptBlock&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$command&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-ArgumentList&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$LHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;***The script looks like this without all the extraneous comments***&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008000;"&gt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Provided for easier reading of the code.&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Import-Module&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;ActiveDirectory&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$adminCreds&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; @{domainA &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Get-Credential&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;domA\admin&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;;domainB &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Get-Credential&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;domB\admin&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;} &lt;/span&gt;&lt;span style="color: #008000;"&gt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;Hash table woohoo!!!&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$user&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Get-ADUser johndoe -Properties memberof -Credential &lt;/span&gt;&lt;span style="color: #800080;"&gt;$adminCreds&lt;/span&gt;&lt;span style="color: #000000;"&gt;.domainA&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$groups&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$user&lt;/span&gt;&lt;span style="color: #000000;"&gt;.memberof | &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;where&lt;/span&gt;&lt;span style="color: #000000;"&gt;{&lt;/span&gt;&lt;span style="color: #800080;"&gt;$_&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;-match&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;dc=domainB&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #800080;"&gt;$s&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;New-PSSession&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-ComputerName&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800000;"&gt;domB-dc1&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-Credential&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$adminCreds&lt;/span&gt;&lt;span style="color: #000000;"&gt;.domainB&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$groups&lt;/span&gt;&lt;span style="color: #000000;"&gt;){&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$LHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; @{userDN &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$user&lt;/span&gt;&lt;span style="color: #000000;"&gt;.distinguishedName;groupDN &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;} &lt;/span&gt;&lt;span style="color: #008000;"&gt;#&lt;/span&gt;&lt;span style="color: #008000;"&gt;another hash table woohoo woohoo!!!&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #800080;"&gt;$command&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; {&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;param&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$multivars&lt;/span&gt;&lt;span style="color: #000000;"&gt;)&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$RHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$multivars&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #FF0000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; [&lt;/span&gt;&lt;span style="color: #008080;"&gt;adsi&lt;/span&gt;&lt;span style="color: #000000;"&gt;]&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;LDAP://$($RHparams.groupDN)&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;.putEx(&lt;/span&gt;&lt;span style="color: #000000;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;member&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #000000;"&gt;,@(&lt;/span&gt;&lt;span style="color: #800080;"&gt;$RHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt;.userDN))&lt;br /&gt;        &lt;/span&gt;&lt;span style="color: #800080;"&gt;$group&lt;/span&gt;&lt;span style="color: #000000;"&gt;.setinfo()&lt;br /&gt;    } &lt;br /&gt;    &lt;/span&gt;&lt;span style="color: #5F9EA0; font-weight: bold;"&gt;Invoke-Command&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-Session&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$s&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-ScriptBlock&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$command&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #5F9EA0; font-style: italic;"&gt;-ArgumentList&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;$LHparams&lt;/span&gt;&lt;span style="color: #000000;"&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-88946440731028293?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/88946440731028293/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/remove-user-in-domaina-from-group-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/88946440731028293'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/88946440731028293'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/remove-user-in-domaina-from-group-in.html' title='Remove a user in domainA from a group in domainB'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-6152846713325091655</id><published>2009-12-15T15:03:00.000-08:00</published><updated>2009-12-15T15:10:38.943-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ActiveDirectory'/><category scheme='http://www.blogger.com/atom/ns#' term='vbscript'/><category scheme='http://www.blogger.com/atom/ns#' term='Powershell'/><category scheme='http://www.blogger.com/atom/ns#' term='lastlogontimestamp'/><title type='text'>LastLogonTimeStamp</title><content type='html'>Hey VBScripters,&lt;br /&gt;&lt;br /&gt;Do you like working with LastLogonTimeStamp (remember this):&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;span style="font: 10pt Courier New;"&gt;&lt;span class="vbscript1-reservedword"&gt;Set&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;oUser&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;=&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-systemfunctionsandvariables"&gt;GetObject&lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;(&lt;/span&gt;&lt;span class="vbscript1-string"&gt;&amp;quot;LDAP://&amp;quot;&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;&amp;amp;&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;sUserDN)&lt;br /&gt;&lt;/span&gt;&lt;span class="vbscript1-reservedword"&gt;Set&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;oLastLTS&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;=&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;oUser.&lt;/span&gt;&lt;span class="vbscript1-reservedword"&gt;Get&lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;(&lt;/span&gt;&lt;span class="vbscript1-string"&gt;&amp;quot;lastlogontimestamp&amp;quot;&lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;)&lt;br /&gt;iLastLogon&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;=&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;oLastLTS.HighPart&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;*&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;(&lt;/span&gt;&lt;span class="vbscript1-number"&gt;2&lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;^&lt;/span&gt;&lt;span class="vbscript1-number"&gt;32&lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;)&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;+&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;oLastLTS.LowPart&lt;br /&gt;iLastLogon&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;=&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;iLastLogon&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;/&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;(&lt;/span&gt;&lt;span class="vbscript1-number"&gt;60&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;*&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-number"&gt;10000000&lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;)&lt;br /&gt;iLastLogon&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;=&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;iLastLogon&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;/&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-number"&gt;1440&lt;br /&gt;&lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;dLastLogon&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;=&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-identifier"&gt;iLastLogon&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-symbol"&gt;+&lt;/span&gt;&lt;span class="vbscript1-space"&gt; &lt;/span&gt;&lt;span class="vbscript1-string"&gt;#1/1/1601#&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;Here is how this works in Powershell v2 RTM using the ActiveDirectory module:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: purple;"&gt;$ADUser&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; Get-ADUser johndoe -Properties lastlogontimestamp&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;if&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: purple;"&gt;$ADUser&lt;/span&gt;&lt;span style="color: black;"&gt;.lastlogontimestamp &lt;/span&gt;&lt;span style="color: red;"&gt;-ne&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: purple;"&gt;$null&lt;/span&gt;&lt;span style="color: black;"&gt;){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: purple;"&gt;$lastlogon&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: cadetblue; font-weight: bold;"&gt;Get-Date&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: cadetblue; font-style: italic;"&gt;-Date&lt;/span&gt;&lt;span style="color: black;"&gt; ([&lt;/span&gt;&lt;span style="color: teal;"&gt;DateTime&lt;/span&gt;&lt;span style="color: black;"&gt;]::&lt;/span&gt;&lt;span style="color: saddlebrown;"&gt;FromFileTime&lt;/span&gt;&lt;span style="color: black;"&gt;([&lt;/span&gt;&lt;span style="color: teal;"&gt;Int64&lt;/span&gt;&lt;span style="color: black;"&gt;]::&lt;/span&gt;&lt;span style="color: saddlebrown;"&gt;Parse&lt;/span&gt;&lt;span style="color: black;"&gt;(&lt;/span&gt;&lt;span style="color: purple;"&gt;$ADUser&lt;/span&gt;&lt;span style="color: black;"&gt;.lastlogontimestamp))) &lt;/span&gt;&lt;span style="color: cadetblue; font-style: italic;"&gt;-Format&lt;/span&gt;&lt;span style="color: black;"&gt; MM/dd/yyyy&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/span&gt;&lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;span style="color: black;"&gt; {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: purple;"&gt;$lastlogon&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: cadetblue; font-weight: bold;"&gt;Get-Date&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: cadetblue; font-style: italic;"&gt;-Date&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: purple;"&gt;$ADUser&lt;/span&gt;&lt;span style="color: black;"&gt;.created &lt;/span&gt;&lt;span style="color: cadetblue; font-style: italic;"&gt;-Format&lt;/span&gt;&lt;span style="color: black;"&gt; MM/dd/yyyy&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;I think that is just so elegant.&amp;nbsp; If I hadn't added the checks and the formatting (like I didn't do in the VB script) this would have only taken two lines.&amp;nbsp; Cool huh?&lt;br /&gt;&lt;br /&gt;Have fun,&lt;br /&gt;Cameron out.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-6152846713325091655?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/6152846713325091655/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/lastlogontimestamp.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/6152846713325091655'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/6152846713325091655'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/lastlogontimestamp.html' title='LastLogonTimeStamp'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-3409561956935208198</id><published>2009-12-15T10:56:00.000-08:00</published><updated>2010-01-15T14:50:29.471-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='function'/><category scheme='http://www.blogger.com/atom/ns#' term='AddDays'/><category scheme='http://www.blogger.com/atom/ns#' term='get-childitem'/><category scheme='http://www.blogger.com/atom/ns#' term='where-object'/><category scheme='http://www.blogger.com/atom/ns#' term='Get-Date'/><category scheme='http://www.blogger.com/atom/ns#' term='Powershell'/><title type='text'>Get Latest File function</title><content type='html'>Our HR department dumps an employee file that contains the latest listing of employees in the company to the same folder each week.  The file contains their employee ID, their Manager, and their manager's employee ID.  The following function allows me to get the lastest file from any folder in the last x number of days.  &lt;br /&gt;&lt;div id="codeSnippet" style="border-style: none; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 10pt;"&gt;&lt;pre&gt;&lt;span style="color: blue;"&gt;function&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: cadetblue;"&gt;Get-LatestFile&lt;/span&gt;&lt;span style="color: black;"&gt;([&lt;/span&gt;&lt;span style="color: teal;"&gt;string&lt;/span&gt;&lt;span style="color: black;"&gt;]&lt;/span&gt;&lt;span style="color: purple;"&gt;$FilePath&lt;/span&gt;&lt;span style="color: black;"&gt;, [&lt;/span&gt;&lt;span style="color: teal;"&gt;int&lt;/span&gt;&lt;span style="color: black;"&gt;]&lt;/span&gt;&lt;span style="color: purple;"&gt;$NumDaysAgo&lt;/span&gt;&lt;span style="color: black;"&gt;){&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: purple;"&gt;$DateToCompare&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; (&lt;/span&gt;&lt;span style="color: cadetblue; font-weight: bold;"&gt;Get-date&lt;/span&gt;&lt;span style="color: black;"&gt;).AddDays(&lt;/span&gt;&lt;span style="color: red;"&gt;-&lt;/span&gt;&lt;span style="color: purple;"&gt;$NumDaysAgo&lt;/span&gt;&lt;span style="color: black;"&gt;)&lt;br /&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;return&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: cadetblue; font-weight: bold;"&gt;get-childitem&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: purple;"&gt;$FilePath&lt;/span&gt;&lt;span style="color: black;"&gt; | &lt;/span&gt;&lt;span style="color: cadetblue; font-weight: bold;"&gt;where-object&lt;/span&gt;&lt;span style="color: black;"&gt; {&lt;/span&gt;&lt;span style="color: purple;"&gt;$_&lt;/span&gt;&lt;span style="color: black;"&gt;.lastwritetime –gt &lt;/span&gt;&lt;span style="color: purple;"&gt;$DateToCompare&lt;/span&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="color: black;"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/div&gt;Use the function as follows:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color: purple;"&gt;$LatestFile&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;=&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: cadetblue;"&gt;Get-LatestFile&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: maroon;"&gt;\\server\share\&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: black;"&gt; &lt;/span&gt;&lt;span style="color: black;"&gt;7&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So basically you supply the folder location and the time frame.&amp;nbsp; The above command will look for the latest file in the past seven (7) days in the location specified.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-3409561956935208198?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/3409561956935208198/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/span-font-family-courier-new-font-size.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/3409561956935208198'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/3409561956935208198'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/span-font-family-courier-new-font-size.html' title='Get Latest File function'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7626999270234232842.post-3542295868313814155</id><published>2009-12-15T07:01:00.000-08:00</published><updated>2009-12-17T22:50:12.701-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Powershell v2'/><title type='text'>PowerShell Blogging</title><content type='html'>Well there are a number of really good resources out there for PowerShell and I've been using them frequently.  However, I've noticed that not a lot of people are using PowerShell v2 RTM that comes with Windows 7 to manage Active Directory.  I'm hoping this can become a place where AD and PowerShell V2 can come together to help administrators take advantage of some of the exciting technologies that are available.  I'm new to blogging and I'm a 'newbie' with PowerShell.  I've spent the last decade VBScripting all over the place now I'm really focused on learning and taking advantage of all the power PowerShell has to offer.&lt;br /&gt;&lt;br /&gt;I have to say to all you VBScripters.  PowerShell is really a superior scripting platform.  I didn't believe it at first but over the past few weeks I'm convinced that I took way too long to come on over to the PowerShell side of scripting. &lt;br /&gt;&lt;br /&gt;My problems with PS were:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;I don't like curly brackets {}.&lt;/li&gt;&lt;li&gt;I didn't like putting $ in front of my variables.&lt;/li&gt;&lt;li&gt;I really thought I had to pipe everything.  Actually, you don't have to pipe anything if (like me) you don't find that sort of thing intuitive.  You can write straight script just like you would in VBScript.&lt;/li&gt;&lt;li&gt;Almost forgot--I really didn't like that comparison operators like = &amp;lt; &amp;gt; were replaced with -eq -lt -gt. &lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;Those things may seem silly to some, but an honest evaluation of my reasoning produced that list as to why I refused to switch to Posh sooner.  Now that I've been using it for about three weeks I'm hooked.  It really is a superior scripting language.  I find myself being able to do all of the things I did in VB plus more and in a more elegant way.  I'm glad I switched.&lt;br /&gt;&lt;br /&gt;If like me you have problems with the syntax, I can tell you that if you just start using PowerShell you will get used to the syntax fairly quickly.  Now curly brackets don't bother me one bit and I'm already used to the $ variables.  So trust me, start using PowerShell, you'll love it.&lt;br /&gt;&lt;br /&gt;I figure with this blog I'll post some of my projects and share what I learn as I learn it.  Hopefully, it'll be useful to someone out there.&lt;br /&gt;&lt;br /&gt;Happy scripting...&lt;br /&gt;&lt;br /&gt;Cameron Out.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7626999270234232842-3542295868313814155?l=blog-powershell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blog-powershell.blogspot.com/feeds/3542295868313814155/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/powershell-blogging.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/3542295868313814155'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7626999270234232842/posts/default/3542295868313814155'/><link rel='alternate' type='text/html' href='http://blog-powershell.blogspot.com/2009/12/powershell-blogging.html' title='PowerShell Blogging'/><author><name>Cameron Ove</name><uri>http://www.blogger.com/profile/07332062383899059988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='28' height='32' src='http://2.bp.blogspot.com/_eZZjQNloui0/S9tUXLO4UmI/AAAAAAAAAEc/wfAY7zMK-pA/S220/DSC_0010-3.jpg'/></author><thr:total>0</thr:total></entry></feed>
