PowerShell Fundamentals
2.1 The PowerShell CLI
For our first task, we should become familiar with the PowerShell Command Line Interface (CLI).
The PowerShell CLI provides us with access to built-in cmdlets, modules, functions, features, and provides a way to create tasks, functions, variables interactively, and more, directly from the CLI.
In most cases, accessing the CLI is as simple as just typing âpowershellâ in the Windows search field from the Start Menu. Alternatively, the shortcut to PowerShell can be found within the â%appdata%\Microsoft\Windows Start Menu\Programs\Windows PowerShellâ directory
In this case, the shortcuts are unavailable; the PowerShell executable itself can found in the âC:\Windows\System32\WindowsPowerShell\v1.0â directory. If other versions are available on the system, they can be found in their corresponding version paths.
If youâre operating on a 64-bit system, the location of the 64-bit PowerShell executable can be found in C:\windows\system32\WindowsPowerShell. While the 32-bit version being located in the C:\windows\SysWOW64\WindowsPowerShell directory. This can be a bit confusing considering the directory naming convention.
Nonetheless, we can determine whether weâre running in a 32-bit or 64-bit PowerShell environment from the CLI with the following command:
PS C:> [Environment]::Is64BitProcessWhich should return âTrueâ if the current PowerShell process is 64-bit
On a 32-bit system, the executable will be in its usual location of: C:\Windows\System32\WindowsPowerShell*
When possible, we should try and launch PowerShell as the Administrator user as this will give us access to functions which we would be otherwise unable to access as a Lower-Privileged user. We can right-click on the Shortcut or Executable, and select âRun As Administrator.â
Although our examples are shown on Windows 10, all of the steps will be similar for Windows 7 and other versions of Windows, unless otherwise noted.
Like most other programs found on Windows, the PowerShell executable has its own set of command line options. We can view these options with the usual â/?â help parameter:
PS C:\Users\Offset> powershell /?
PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
[-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
[-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
[-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
[-ConfigurationName <string>]
[-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
[-Command { - | <script-block> [-args <arg-array>]
| <string> [<CommandParameters>] } ]These options can alternatively be shown with the â-Helpâ parameter or â-?â as well and will be most useful when weâre calling PowerShell from a standard Windows command prompt. (cmd.exe)
The following are some of the more common PowerShell.exe command line parameters we will use for our purposes: The PowerShell execution policy determines which scripts if any, we can run and can easily be disabled with the âBypassâ or âUnrestrictedâ arguments.
The -WindowStyle parameter hides the Powershell window when used with the âhiddenâ argument.
The -Command parameter is used to specify a Command or Script Block to run.
The -EncodedCommand parameter is used to execute base64 encoded scripts or commands.
Donât load any powershell profiles. Profiles are essentially scripts that run when the powershell executable is launched and can interfere with our operations.
We can use the -Version parameter followed by a version number as the argument to downgrade the version of PowerShell.
Furthermore, all of the PowerShell.exe command line parameters, as well as their arguments, can also be abbreviated, as long as the abbreviations are unique, and additionally, are not required to be case-sensitive either:
An extremely useful feature of the PowerShell CLI is the âGet-Helpâ cmdlet. Similar to *nix âMan Pages,â we can call upon the âGet-Helpâ command to obtain information related to any function, alias, module or cmdlet that PowerShell is aware of.
We can do this by including the cmdlet, function or module name weâre looking for information on, as an argument to the âGetHelpâ cmdlet.
We can see here that weâre requesting the PowerShell Help pages for the âGet-Helpâ cmdlet itself:
To get âfullâ help for any cmdlet, which includes detailed information on associated parameters, we can use the - Full parameter:
And if weâd like to get examples on how to use a specific cmdlet, we can use the â-Examplesâ parameter.
Alternatively, if we want to get current Help pages from online for any of the cmdlets or Functions, we can simply supply the -Online parameter to our command line, and will launch a web browser to the corresponding help page:
If weâd like to update our locally installed help files for PowerShell via the CLI, we can do so with the âUpdate-Helpâ cmdlet:
More information on using the âGet-Helpâ cmdlet can be found here:
The âGet-Commandâ cmdlet is another very useful one. It allows us to list all cmdlets, aliases, functions, workflows, filters, scripts and any applications that are available for us to use in PowerShell.
Running the âGet-Commandâ cmdlet without arguments will simply list all commands, but, we can also use the -Name parameter to list any that are useful to us. For instance, we can list all functions related to modification of the Windows Firewall with the following command:
Last updated