2 Explore PowerShell
2 Explore PowerShell
To complete the labs for this class you will need to create a virtual machine
as described in the lab environment setup instructions here.
In this lab, we will explore the PowerShell command shell, the place where
you can run scripts and interactively enter commands. PowerShell is easy
to use when you understand the basic building blocks and how they work
together.
Start the PowerShell application using the shortcut on the desktop of your
lab VM.
Starting PowerShell this way starts PowerShell with normal user privileges
(not admin privileges). Start a second PowerShell command shell by right-
clicking on the PowerShell desktop shortcut and choosing “Run as
Notice that the PowerShell application that you “Ran as Administrator” has
the word “Administrator” in the title. There are several times throughout the
labs that you will need to use an administrative PowerShell prompt, so pay
close attention to the instructions and the screenshots.
Variables
Let’s explore some of the features of the PowerShell command shell.
Variables in PowerShell start with a $ sign. Try defining some variables and
then echoing them back onto the screen either by entering only the variable
name and pressing enter or by using the Write-Host function.
We are using PowerShell version 5.1 in class. This is the version that
comes installed by default on Windows 10 and 11.
Session History
For convenience, the PowerShell command prompt also maintains a list of
all the commands you have run in the current session. You can access this
list using the Get-History function (or just h for short).
Start a PowerShell window and run the Get-History command. Do you see
any session history? No, the session in this case will be empty because we
have started a new session. If we are wanting to reference commands from
our command history across sessions, or even computer restarts, we need
to use the file-base history as discussed next.
We can view and set the configuration for the PSReadline module using
the Get-PSReadlineOption and Set-PSReadlineOption commands.
You can use the following command to print the contents of the history file
to the screen.
Providers
PowerShell uses Providers to make working with “file-like” systems
convenient and consistent. View the providers with the Get-PSProvider
function.
You can make use of Providers with functions like Get-ChildItem, Set-
ChildItem and more. We are accustomed to using FileSystem providers
such as the C:\ drive on a computer as shown below.
Above you can see how we used PowerShell’s registry provider to list
Control Panel setting for the current user (HKCU). The image to the right
shows the same registry setting displayed in the Registry Editor tool for
comparison.
Get-ChildItem variable:
You will notice the output is the same as when we used the Get-Variable
function. This is because the Get-Variable function simply prints the data in
the variable drive.
Now let’s take a closer look at the Alias and Env providers/drives.
Aliases
Aliases are shortcuts to functions. There are many built-in and you can also
create custom ones. View the aliases in the current session with the Get-
Alias function. Of course, you could also use Get-ChildItem alias:
because it is one of PowerShell’s built-in Providers.
Use the Set-Alias function to create your own aliases. Here I create an
alias for Get-Date so I can simply type gd to get the date.
If you want to set an alias for something more complex, you can create an
alias to a function as follows.
Environment Variables
The Environment Provider gives us the env: drive which we can use to
view and set environment variables used by the operating system. How do
you think we can view this drive? You guessed it …
One of the environment variables you will see used in PowerShell often is
TEMP, which is the path to the temporary directory.
The env drive is implemented is such a way that you can refer to it using
the following short hand. This makes it very convenient to use.
Save your new profile in notepad and then start a new PowerShell session
to see the effect.
Execution Policy
Bypass AllSigned
Restricted Default
Unrestricted Undefined
RemoteSigned
The default execution policy for Windows 10 clients is Restricted but for
some reason the Microsoft test VMs for our labs has it set to
RemoteSigned.
In our lab environment, we don’t need to fight with the execution policy
setting but here is the link to 15 ways to bypass PowerShell execution
policy if you find yourself in need of that in the future. Remember that the