Saturday, January 9, 2016

Powershell is Being a Pain ("Get-ADUser is not recognized...")

While diagnosing some performance issues on a virtual machine, I decided to rebuild my Windows 7 VM. Reload, update (and update and update), and begin the process of reinstalling applications and tweaking preferences.

One of the applications I've grown accustomed to using is a small Powershell script that gives a brief list of stats on a user's Active Directory account. It's pretty handy for quickly figuring out if Bob forgot to reset his password or his account is now locked out from too many incorrect password attempts.

My old machine just ran the script without issues. The new one, however, wanted nothing to do with it.
The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. How annoying...

Turns out lots of other people have that issue too, if the Google search results are any indication.

I had the Remote Server Administration Tools installed for Windows 7 (KB958830), the prerequisite for running the Get-ADUser module.

The second step was to actually enable the feature, which is, counter-intuitively, then added as a Windows feature.
  1. Click the start menu and search for "windows features", click on "Turn Windows features on or off"
  2. Expand Remote Server Administration Tools
  3. Expand Role Administration Tools
  4. Expand AD DS and AD LDS Tools
  5. Tick the checkbox for Active Directory Module for Windows Powershell
  6. Confirm that it's okay to install the feature
And you know what? It still didn't work.

BUT I discovered that the module did exist; if I opened a command prompt and first ran "import-module activedirectory," then manually ran the script instead of right-clicking the script and clicking "Run with Powershell" everything worked.

So how do I get it to run by automatically loading that module?

From a Powershell prompt I ran "$profile" to discover where my PowerShell profile is located; it returned 

C:\Users\bsilverstrim\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

I opened an Explorer window and navigated to C:\Users\bsilverstrim\Documents. There was no WindowsPowerShell folder.

Next step: make the folder.

Then in that folder I used a text editor to create a file called Microsoft.PowerShell_profile.ps1. In it, I had the line:

import-module activedirectory

I saved the file and closed out of my open PowerShell sessions. Make sure the suffix on that text file is .ps1, as Windows likes to try hiding a .txt extension if you use the common method for creating new text files. I right-clicked the Powershell script on my desktop, ran it with PowerShell, and it worked fine!

1 comment:

  1. Thank you for your post, this worked and saved me some time. You are a God among men.

    ReplyDelete