Understanding PowerShell Advanced Concepts
(Which Google refuses to show you)

Dr Scripto and Slowly

Providers

A Provider is a kind of driver for powershell, like the filesystem provider, the registry provider, and the like. The Powershell Gallery requires 2 providers to be usable: PackageManagement and PowerShellGet. A successful installation of PowerShellGet will install both of these providers.

Packages

A Package can be anything. Packages are grouped into package Types. Package types supported by the Powershell platform include Modules, Scripts, and ISE Addons.

The Install-Package command is part of the lower-level PowerShell Package Manager (PackageManagement). It can use any available provider.

Addons

An Addon is a 'plugin' for the powershell ISE. By default, they are stored in the $HOME\Addons folder. An example is the ModuleBrowser addon, not to be confused with the command browser.

Modules

A Module is a package that contains PowerShell commands, such as cmdlets, providers, functions, workflows, variables, and aliases. The Install-Module command is a very specific implementation that rides atop PowerShell Package Manager (PackageManagement). It only installs PowerShell artifacts, and works only with providers that look and feel like PowerShell Gallery providers, but they can be public or private.

  • Get-InstalledModule will list modules installed using the Install-Module command.
  • Get-Module -ListAvailable shows modules from all paths in the $env:PsModulePath environment variable.

Profiles

A Windows PowerShell profile is simply a Windows PowerShell script. It has a special name, and it resides in a special place, but it is simply a script. In this regard, it is sort of like the old-fashioned autoexec.bat batch file. Because the Windows PowerShell profile is a Windows PowerShell script, you must enable the Script Execution policy prior to configuring and using a Windows PowerShell profile, like this:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
     

There are, in fact, six different profiles. The Windows PowerShell console and the Windows PowerShell ISE have their own profiles. In addition, there are profiles for the current user and profiles for all users. The table that follows lists the six profiles and their associated locations.

Description Path
Current User, Current Host – console $Home\[My ]Documents\WindowsPowerShell\Profile.ps1
Current User, All Hosts $Home\[My ]Documents\Profile.ps1
All Users, Current Host – console $PsHome\Microsoft.PowerShell_profile.ps1
All Users, All Hosts $PsHome\Profile.ps1
Current user, Current Host – ISE $Home\[My ]Documents\WindowsPowerShell\Microsoft.P owerShellISE_profile.ps1
All users, Current Host – ISE $PsHome\Microsoft.PowerShellISE_profile.ps1

The first thing to do to understand the six Windows PowerShell profiles is to keep in mind that they move. They change (sort of like the staircases at Hogwarts). As long as you realize that they are a moving target, you will be fine. In most cases, when talking about the Windows PowerShell profile, people are referring to the current user, currenthost profile. In fact, if no one qualifies the Windows PowerShell profile with its associated scope or description, it is safe to assume that they are talking about theCurrent User, Current Host profile.

Examining the $profile variable

When you query the $profile automatic variable, it returns the path to the Current User, Current Host profile. This makes sense, and it is a great way to easily access the path to the profile. The following script illustrates this technique from within the Windows PowerShell console.

PS C:\> $profile

C:\Users\ed.IAMMRED\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Inside the Windows PowerShell ISE, when I query the $profile automatic variable, I receive the output that is shown here.

PS C:\Users\ed.IAMMRED> $profile

C:\Users\ed.IAMMRED\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

To save you a bit of analyzing…

The difference between the Windows PowerShell console Current User, Current Host profile path and the Windows PowerShell ISE Current User, Current Host profile path is three letters: ISE.

BB, these three letters are probably causing you problems. More than likely, you are setting something in your Windows PowerShell console profile, and it is not available inside the Windows PowerShell ISE.

Unraveling the profiles

You can pipe the $profile variable to the Get-Member cmdlet and see additional properties that exist on the $profile variable. This technique is shown here.

PS C:\> $PROFILE | Get-Member -MemberType noteproperty | select name

Name

—-

AllUsersAllHosts

AllUsersCurrentHost

CurrentUserAllHosts

CurrentUserCurrentHost

If you are accessing the $profile variable from within the Windows PowerShell console, the AllUsersCurrentHost and the CurrentUserCurrentHost note properties refer to the Windows PowerShell console. If you access the $profile variable from within the Windows PowerShell ISE, the AllUsersCurrentHost and the CurrentUserCurrentHost note properties refer to the Windows PowerShell ISE profiles.

Using the $profile variable to refer to more than the current host

When you reference the $profile variable, by default it refers to the Current User, Current Host profile. If you pipe the variable to the Format-List cmdlet, it still refers to the Current User, Current Host profile. This technique is shown here.

PS C:\> $PROFILE | Format-List *

C:\Users\ed.IAMMRED\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

This leads to a bit of confusion, especially because the Get-Member cmdlet reveals the existence of multiple profiles and multiple note properties. The way to see all of the profiles for the current host, is to use the force parameter. It reveals the hidden properties. The command illustrating this technique is shown here.

$PROFILE | Format-List * -Force

The command and the associated output from the command are shown in the image that follows.

It is possible to directly access each of these specific properties just like you would access any other property—via dotted notation. This technique is shown here.

$PROFILE.CurrentUserAllHosts

The path to each of the four profiles for the Windows PowerShell console are shown in the image that follows.

Determine if a specific profile exists

To determine if a specific profile exists, use the Test-Path cmdlet and the appropriate flavor of the$profile variable. For example, to determine if a Current User, Current Host profile exists you can use the $profile variable with no modifier, or you can use the CurrentUserCurrentHost note property. The following example illustrates both of these.

PS C:\> test-path $PROFILE

True

PS C:\> test-path $PROFILE.CurrentUserCurrentHost

True

PS C:\>

In the same manner, the other three profiles that apply to the current host (in this example, I am using the Windows PowerShell console) are determined to not exist. This is shown in the code that follows.

PS C:\> test-path $PROFILE.AllUsersAllHosts

False

PS C:\> test-path $PROFILE.AllUsersCurrentHost

False

PS C:\> test-path $PROFILE.CurrentUserAllHosts

False

PS C:\>

Creating a new profile

To create a new profile for current user all hosts, use the CurrentUserAllHosts property of the $profile automatic variable, and the New-Item cmdlet. This technique is shown here.

PS C:\> new-item $PROFILE.CurrentUserAllHosts -ItemType file -Force

Directory: C:\Users\ed.IAMMRED\Documents\WindowsPowerShell

Mode LastWriteTime Length Name

—- ————- —— —-

-a— 5/17/2012 2:59 PM 0 profile.ps1

To open the profile for editing, use the ise alias as shown here.

ise $PROFILE.CurrentUserAllHosts

When you are finished editing the profile, save it, close the Windows PowerShell console, reopen the Windows PowerShell console, and test that your changes work properly.