Category: powershell

User Powershell to see which AD groups a users belongs to

For the past upteen years, to get a list of the groups a user belongs to, I’ve used:

> net user /domain userID

Today, I just stumbled upon a powershell cmdlet that does just that.

To get a list of groups a users belongs to run the following at a powershell prompt.

PS c:\ >Get-ADPrincipalGroupMembership userID | select name


Screenprint of Powershell window

Note: You will need the Active Directory Powershell module and that comes from installing RSAT.

Be soical and share.

Compare 2 files with PowerShell

I’m building a new 2012R2 server that will replace an existing 2008R2 application server. I want to see what roles I need to install on the new server, so I run the Get-WindowsFeature cmdlet in PowerShell to give me what I need.

PowerShell Get-WindowsFeature Output

I have saved the output of Get-WindowsFeature to .csv on 2 servers, a new server and the old, production server.

PS> Import-Module servermanager ; get-windowsfeature | where-object {$_.Installed -eq $True} | format-list DisplayName > old-roles.csv

PS> Import-Module servermanager ; get-windowsfeature | where-object {$_.Installed -eq $True} | format-list DisplayName > new-roles.csv

To compare the files, use the compare-object cmdlet.

From Microsoft:

The Compare-Object cmdlet compares two sets of objects. One set of objects is the “reference set,” and the other set is the “difference set.”

PS> compare-object $(Get-Content K:\old-roles.csv) $(Get-content K:\new-roles.csv) -includeequal


Side Indicator Meaning
== Item appears in both files
=> Item only appears in the difference set
<= Item only appears in the reference set

PowerShell compare-object output

The -ReferenceObject and -DifferenceObject options weren’t needed in this example. I got the same output with and without it.

So, on both new and old server, File Server, Remote Server Administration tools, are installed.
The new server has its own default roles and features installed and the old server has the list of roles that must be installed onto the new server, if applicable. The old server is 2008 and the new server is 2012 R2. Many features are now either default (PowerShell) or updated (.NET Framework 4.5). This list isn’t one to one, but it gives me an idea what needs to be installed in order to migrate a service from one OS to another. T

There are many useful applications for diff’ing files. What are some of your most common uses?

In addition:

You can assign the files to variables and use the split() function to get rid everything up to a delimiter and output the ‘newly cleaned’ file to a new file.

$old = Get-Content .\old-roles.csv
$new = Get-Content .\new-roles.csv

Get-Content $old | ForEach-Object {$_.split(":")[1] } > sorted-old-roles.txt
Get-Content $new | ForEach-Object {$_.split(":")[1] } > sorted-new-roles.txt

$newsort = ".\sorted-new-roles.txt"
$oldsort = ".\sorted-old-roles.txt"
Compare-Object $(Get-content $oldsort) $(Get-Content $newsort) -includeequal

Split Fucnction results

Of course, just using the diff command on both files in Linux would have given me what I needed in 5 seconds, but as we know, it’s all about learning the process.

Linux diff output
Linux would have made comparing the files a little bit easier
Gutter Markings Meaning
White space These lines are common in both files. In both files, you have chicken breast, tofu and naan.
> The files differ here. the second file listed in the command has this entry. The first file doesn’t, like milk, eggs, bread and broccoli.
< The files differ. The first file only has this entry. The second file doesn’t, like avocado, almond mil and peanut butter.
| These lines differ, meaning if we were to merge the two files, we’d have to resolve if we want to keep one or both entries.
Be soical and share.

-ShowWindow in Powershell help

The adage, you learn something new everyday is very true.

I’m reading a book called Learn Powershell in a Month of Lunches by Don Jones and Jeff Hicks and I just discovered a helpful nugget I didn’t know existed, -ShowWindow.

help Get-ADGroupMember -showwindow

The -ShowWindow parameter will give you a popup window with the help topic you’re researching.  You can search words and phrases within the help topic. The found term is highlighted for easy reading and you can move between terms using the previous and next buttons. The window can be resized and you can increase or decrease the text with the slider at the bottom of the window. The description is a bit shorter for some cmdlets, but there are even some command examples displayed in the help window to get you going without coming out of your prompt.

The PowerShell help window

Gone are the days of opening a second Powershell window to reference the help while crafting command line syntax. -ShowWindow is a great too in the Powershell arsenal.


~Note~ This works differently in PS 4.0 and 5.0. and within different builds of Windows 10. 
Some contents are missing or out of order. It appears it is a known issue.
Your mileage may vary.

A curved arrow pointing right Don’t forget Help Cmdletname -online It launches help in the browser. Keeping you in your PS window without taking you away from your prompt.


Be soical and share.

Check the hash on a file using PowerShell

Checking the hash of a file you’ve downloaded from the internet is very good practice. It’s a way to ensure the file you’re downloading hasn’t been tampered with, according to the hash provided on the site you’re downloading from.

Since Powershell 4.0, Get-FileHash has been a way to check the hash signature of a file.

A powershell windows showing the Get-FileHash command

Get-FileHash -Path C:\Path\to\file\file.exe -Algorithm SHA256

Get-FileHash can check the following signatures:

  • SHA1
  • SHA256 (default value)
  • SHA384
  • SHA512
  • MACTripleDES
  • MD4
  • RIPEMD160
Be soical and share.
Social media & sharing icons powered by UltimatelySocial