PowerShell 101

Why bother learning PowerShell?

That was a question I struggled with for a long time. I've been using Dos/cmd since middle school and bash for more than twenty years. I know them well enough to feel like there isn't much that I might want to do with commands that I can't already do well with my shell of choice. Yet, there was an allure to being able to work with Excel without needing to install specialty tools. I already could interact with Active Directory but the PowerShell examples were much more intuitive than the methods I would have used otherwise. Hyper-V was the clincher for me. We have several Core installs and being able to work on them without needing to install extra software would mean that I could stand in front of any of them and accomplish my goals without having to resort to a lot more tedious research.

So I took the plunge and have been quite happy with the results.

What to look forward to

PowerShell has most of the standard tasks you're used to using on a command line. Many of these have aliases that you'll see me use. A surprising number of them match Unix/Linux commands. 

Take copying a file:

Copy-Item -Path "C:\temp\picture.png" -Destination "C:\temp\newplace.png"

or Unix style:

cp "C:\temp\picture.png" "C:\temp\newplace.png"

You can see why I might prefer the Unix style. You can get a list of available aliases with the Get-Alias command.

Other Unix style aliases I use frequently:

Move-Item aka mv

Remove-Item aka rm

Copy-Item aka cp

Write-Output aka echo

Get-Content aka cat

Tee-Object aka tee

Get-ChildItem aka ls

Then there are a few that I don't recognize as Unix-like, but which save some typing:

ForEach-Object aka %

Format-Table aka ft

Equivalent to tail:

cat -Last 10 -Wait '\\server\c$\temp\serversmonitored.log' 

Bonus: PowerShell does tab auto-complete better than any other shell I've worked with. 

For example, I type "Get-A" and hit tab and it cycles through the commands that I might want. "Get-Acl" is first, then "Get-ADAccountAuthorizationGroup", followed by "Get-ADAccountResultantPasswordReplicationPolicy" and so on. Since I'm looking for Get-Alias, I type "Get-Ali" and it auto-completes. But that's just the beginning. Now I type "-" and hit tab and it starts going through the parameters I might want to specify. So I typed "Get-Ali[tab]-[tab]" and my command line shows "Get-Alias -Name" which is handy when you don't want to look up the parameters. 

But wait, there's more! If you type in "Out-F[tab] -E[tab] [tab]" you get the command "Out-File -Encoding ascii" which is awesome because not only does it provide the parameter "Encoding" it is smart enough to offer "ascii" as the argument and cycle through the other encoding options you might want if you hit tab repeatedly.

What else could you want? Oh, you want file and path expansion? Yup, PowerShell does that too, but not with backslashes for special characters like bash does. Instead it puts those things that need them inside single quotes for you. 

Take this example, I type "cd c:\us[tab]\j[tab]\O[tab][tab]\Do[tab][tab]" and this is what my command line shows:

cd 'C:\Users\bcrownover\OneDrive - America''s Credit Union\Documents - Technology Training\'

That is a lot of typing I just didn't need to do because PowerShell does such nice auto-completion.

Then there is the ability to work with the network paths like they're a local file system.  If I am admin and want to change my directory to that of a server, I do this:

PS C:\WINDOWS\system32> cd \\mr-server\c$

PS Microsoft.PowerShell.Core\FileSystem::\\mr-server\c$>

Easy peasy and the auto-complete works with remote server paths the same way as it does with local.

Try that with good old cmd and it helpfully informs you "CMD does not support UNC paths as current directories."

Even the registry does auto-complete. I type "get-itemp[tab] HKLM:H[tab][tab][Enter]" and I'm looking at this:

PS C:\Users\me> Get-ItemProperty -Path HKLM:\HARDWARE\DESCRIPTION\System\CentralProcessor\1\



Component Information     : {0, 0, 0, 0...}

Identifier                : Intel64 Family 6 Model 142 Stepping 10

Configuration Data        :

ProcessorNameString       : Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz

VendorIdentifier          : GenuineIntel

FeatureSet                : 823869439

~MHz                      : 1896

Update Revision           : {0, 0, 0, 0...}

Previous Update Revision  : {0, 0, 0, 0...}

Update Status             : 6

Platform Specific Field 1 : 128

Platform Specific Field 2 : {0, 0, 0, 0}

PSPath                    : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\1\

PSParentPath              : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor

PSChildName               : 1

PSDrive                   : HKLM

PSProvider                : Microsoft.PowerShell.Core\Registry


Getting Started

While you're just getting started, I suggest you try to think of a handful of commands you commonly use and look up how to do them with your search engine of choice. Check your available aliases to see if there are options that make it easier for you to remember. Then comes the big step. Start using PowerShell instead of the shell you are more familiar with for at least the things you do most commonly.

Once you're used to going to PowerShell it will be easier for you to think of things that might be easy to do if you just take a minute to learn how. My learning experience is scattered all over this section of my website. I've learned as I go and would still classify myself as new to PowerShell, since I have been using other command lines for more than thirty years, but I am happy to say I no longer consider myself a complete novice after just a couple years of trying to use it to accomplish my daily tasks.