PowerShell for the Layperson (It's Me I'm the Layperson)

PowerShell for the Layperson (It's Me I'm the Layperson)

"To command, or not to command? That is question." - Unknown(No-one)

In the Beginning...

PowerShell is a Microsoft developed command line shell and scripting environment that allows you to do a lot - simply put. I have used Bash for many years in both Linux and Windows environments and PowerShell never really crossed my mind to use, but I've decided to take the plunge into learning PowerShell for my journey into The Cloud. A few weeks ago, I earned my AZ-900 certificate which covered Azure fundamentals, and in order to take next steps into other certificates, I must have more familiarity with Azure CLI which works well with PowerShell. So, why not now?

What it do? (No, not what's up. What is it, and what does it do?)

To elaborate on the definition above, PowerShell is both a shell, a screen more simple than a Graphical User Interface where you can type commands that do a multitude of things, and a scripting language that can help with automating tasks and services - similar to Bash. PowerShell is a powerful tool that can be used in various OS environments with the release of PowerShell Core. If you need to be able to view systems services, have a job run at a designated time, or just have a bit of fun at the command line, PS may just be the tool for you! First, let's install it; then, let's go over some of the basic commands for navigating the files system and getting help.

The Install

Okay, okay, hold your horses!

On Windows, from my experience, PS comes installed by default. You can figure out how to download the flavor of PS you'd like from here: PowerShell Install Information

Using a Bit of What I Know

I have found my minute amount of Bash syntax to be helpful. In PS, you can use the following commands to navigate directories and files similar to bash:

ls (list current directory)
cd (change directory)
pwd (print working directory)
cp (copy file)
rm (remove file/directory)
cat (concatenate lines)

These are a few of the commands I found that function in PS because they are aliases for other cmdlets -- compiled commands.

Navigating the PowerShell Seas

Now that we have a bit of familiarity under our belt, let's look at a few commands that can "help" us along the way navigating what we can do with PowerShell.

Get-Help

One of my most used commands thus far has been Get-Help. If it was a person, I know I would be the annoying colleague by now. Get-Help is your go to tool for understanding a cmdlet, object, etc. while using PowerShell.

For example, when getting started I was curious about a few of the aliases above. So, I ran Get-Help on pwd:

Get-Help pwd

Returns:

NAME
    Get-Location

SYNOPSIS
    Gets information about the current working location or a location stack.


SYNTAX
    Get-Location [-PSDrive <System.String[]>] [-PSProvider <System.String[]>] [<CommonParameters>]

    Get-Location [-Stack] [-StackName <System.String[]>] [<CommonParameters>]


DESCRIPTION
    The `Get-Location` cmdlet gets an object that represents the current directory, much like the print working directory (pwd) command.

    When you move between PowerShell drives, PowerShell retains your location in each drive. You can use this cmdlet to find your location in each drive.

    You can use this cmdlet to get the current directory at run time and use it in functions and scripts, such as in a function that displays the current directory in the PowerShell prompt.

    You can also use this cmdlet to display the locations in a location stack. For more information, see the Notes and the descriptions of the Stack and StackName parameters.

You may have to run Update-Help to get full definitions for many of the cmdlets when you first run or install PS, but the help documentation is clear and detailed.

Get-Command

Very similar to the Get-Help command, the Get-Command allows you to search for different cmdlets, aliases, functions, filters, scripts, and applications. Running Get-Command, will list currently installed modules and commands that go along with them. For example:

Get-Command -Verb Start| Where-Object {$_.CommandType -EQ "Alias"}

Will return:

Alias           Start-ASRApplyRecoveryPoint                        3.5.0      Az.RecoveryServices
Alias           Start-ASRCommitFailover                            3.5.0      Az.RecoveryServices
Alias           Start-ASRCommitFailoverJob                         3.5.0      Az.RecoveryServices
Alias           Start-ASRFO                                        3.5.0      Az.RecoveryServices
Alias           Start-ASRPFO                                       3.5.0      Az.RecoveryServices
Alias           Start-ASRPlannedFailoverJob                        3.5.0      Az.RecoveryServices
Alias           Start-ASRResynchronizeReplicationJob               3.5.0      Az.RecoveryServices
Alias           Start-ASRResyncJob                                 3.5.0      Az.RecoveryServices
Alias           Start-ASRSwitchProcessServerJob                    3.5.0      Az.RecoveryServices
Alias           Start-ASRTestFailoverCleanupJob                    3.5.0      Az.RecoveryServices
Alias           Start-ASRTestFailoverJob                           3.5.0      Az.RecoveryServices
Alias           Start-ASRTFO                                       3.5.0      Az.RecoveryServices
Alias           Start-ASRTFOCleanupJob                             3.5.0      Az.RecoveryServices
Alias           Start-ASRUnplannedFailoverJob                      3.5.0      Az.RecoveryServices
Alias           Start-CopyAzureStorageBlob                         3.5.0      Az.Storage

Or something similar. The Where-Object command allows you to filter through the results and see if properties are equal.

The Long Road Ahead

Whew okay. That's only two cmdlets. In upcoming posts I want to cover more in depth aspects, but this is just the beginning. PowerShell has been a logical step for command line work and scripting for me. Can you see yourself using it in the future? What are you hoping to see on the next episode of Drago-- I mean in the next article. Leave your comments below and let me know!

References