The Bare Basics of PowerShell

PowerShell is a powerful tool you can add to your skillset. It has been around since 2006, when it was added to Windows as a task automation and configuration framework.

Powershell used to be only available on Windows, but since 2016 it has become a cross-platform tool that can also be utilized on Macintosh and Linux devices.

This article will go over some basic details about this awesome tool. I will dig deeper into PowerShell in subsequent articles.

Versions

The most popular version of PowerShell (and the one that is cross-platform) is PowerShell 7. This is fine to use for most use-cases; however, some older modules may only run on previous versions. If a module does not install correctly on PowerShell 7, it is recommended to try installing PowerShell 5 as well (both can run on the same system, but note that PowerShell 5 is only available on Windows).

Modules

Just as many programming languages have libraries to make tasks easier to implement, PowerShell contains modules that allow a user do many things, including working with additional cmdlets, functions, APIs, and aliases.

Anyone can write and share a PowerShell module. Some popular modules include the following:

Azure PowerShell: Used to administrate Windows and Azure apps. PowerSharePoint: Used to administer SharePoint. Can automate and create complex rules that are not possible via the GUI admin center. PowerScheduledTasks: Used to set up recurring scripts. These are just a few of the exhaustive list of modules you can find.

Variables

Just as with programming languages, you can utilize variables in PowerShell. PowerShell variables begin with the $ symbol. Here is an example of an assigned variable.

$users = import-csv $filename.fullname

In the above example, we are setting a variable $users that runs the import-csv command from a previously set-variable ($filename.fullname), which gets the full name of a file so that the import-csv command can run when we call $users.

Variables are usually scoped only to their function, but their scope can be changed to scope up to the script or global level. Here is an example of a variable that has been scoped for use throughout the script once it has been initialized (meaning it will retain its value if it is called again, even if the next time it is called its out of its original functional scope).

function test-pathExport {
 $int = $null
 $ACtest = test-path $ACexport
 If ($ACtest -eq $True) {
  foreach ($t in $ACtest) {
   $int = ++$int $script:ACexport = "C:\s\sample.csv" $ACtest = test-path $ACexport
  }
 }
}

There are several variables in the above example! But there is only one variable whose scope is altered:

$script:ACexport = "C:\s\sample.csv"

The expression script: is placed right after the $, but before the variable. This tells PowerShell to use the variable's value throughout the rest of the script if it is called again, even if the variable is outside the function (or inside of another function).

Cmdlets

A cmdlets is a command that you use to tell PowerShell to do something. Each cmdlet has additional rules that can be added to adjust the information that will be provided back to you after running it. There are about 200 standard cmdlets out-of-the-box, but more can be added by importing additional modules to your environment.

Here is a list of 3 basic cmdlets to get you started:

  • Get-Command: Lists all available cmdlets for you to use during your session. Here is an example of output after running Get-Command:
PS C:\Users\s> get-command | select name

Name
----
Add-AppPackage
Add-AppPackageVolume
Add-AppProvisionedPackage
Add-ProvisionedAppPackage
Add-ProvisionedAppxPackage
Add-ProvisioningPackage
  • Get-Help: Used in conjecture with another cmdlet, provides detailed information about the commands of the cmdlet. Here is an example of running Get-Help for the Get-Command cmdlet:
PS C:\Users\s> get-help get-command

NAME
Get-Command

SYNTAX
Get-Command [[-ArgumentList] <Object[]>] [-Verb <string[]>] [-Noun <string[]>] [-Module <string[]>]
[-FullyQualifiedModule <ModuleSpecification[]>] [-TotalCount <int>] [-Syntax] [-ShowCommandInfo] [-All]
[-ListImported] [-ParameterName <string[]>] [-ParameterType <PSTypeName[]>] [<CommonParameters>]
  • Get-Service: Shows all services installed on your system. You can also filter out which services to show (such as Stopped or Running). Here's a sample of the out-put of running Get-Service (selecting only the "Name" attribute):
PS C:\Users\s\Documents\code> get-service | select name

Name
----
AarSvc_e19d7
AdobeARMservice
AdobeUpdateService

Conclusion

This is a very basic overview of PowerShell. For the next article, I'll plan on covering the PowerShell $profile - what it is, and how to set it up.