Exploring Installation Artifacts
Installing PowerShell 7 creates some artifacts that may be useful to better understand how PowerShell 7 and Windows PowerShell differ. The installation folder for PowerShell, as well as the folders holding PowerShell modules, are different from Windows PowerShell.
Getting ready
You run this recipe on SRV1
after you have installed PowerShell 7 and have added a profile file.
How to do it...
- Checking the version table for the PowerShell 7 console
$PSVersionTable
- Examining the PowerShell 7 installation folder
Get-ChildItem -Path $env:ProgramFiles\PowerShell\7 -Recurse | Measure-Object -Property Length -Sum
- Viewing the PowerShell 7 configuration JSON file
Get-ChildItem -Path $env:ProgramFiles\PowerShell\7\powershell*.json | Get-Content
- Checking the initial Execution Policy for PowerShell 7
Get-ExecutionPolicy
- Viewing module folders
$I = 0 $ModPath = $env:PSModulePath -split ';' $ModPath | Foreach-Object { "[{0:N0}] {1}" -f $I++, $_ }
- Checking the modules
$TotalCommands = 0 Foreach ($Path in $ModPath){ Try { $Modules = Get-ChildItem -Path $Path -Directory -ErrorAction Stop "Checking Module Path: [$Path]" } catch [System.Management.Automation.ItemNotFoundException] { "Module path [$path] DOES NOT EXIST ON $(hostname)" } $TotalCommands = 0 foreach ($Module in $Modules) { $Cmds = Get-Command -Module ($Module.name) $TotalCommands += $Cmds.Count } }
- Viewing the totals of the commands and modules
$Mods = (Get-Module * -ListAvailable | Measure-Object).count "{0} modules providing {1} commands" -f $Mods,$TotalCommands
How it works...
In step 1, you start the PowerShell 7 console on SRV1
. The console should look like this:
Figure 1.23: Checking PowerShell Version information
In step 2, you use Measure-Object
to determine how many files exist in the PowerShell installation folder and how much space those files occupy on the disk. The output of this step looks like this:
Figure 1.24: Examining the PowerShell 7 installation folder
PowerShell 7 uses the PWSH.JSON
file (in the installation folder) to hold certain key settings. In step 3, you examine this file for PowerShell 7.2.2, with output like this:
Figure 1.25: Viewing the PWSH.JSON configuration file
In step 4, you check the current execution policy for PowerShell 7, with output as follows:
Figure 1.26: Checking the PowerShell 7 execution policy
PowerShell 7, by default, loads modules from a series of folders as described in the PowerShell variable $PSModulepath
variable. In step 5, you display the default module folders for PowerShell 7, with output like this:
Figure 1.27: Viewing the module folders for PowerShell 7
In step 6, you look at the modules in each module path and determine the total number of commands available via these modules. The output from this step looks like this:
Figure 1.28: Counting the commands available
In the final step in this recipe, step 7, you view the number of modules and commands provided in those modules, with output like this:
Figure 1.29: Counting the commands available
There’s more...
In step 1, you open a new Windows PowerShell console and view the PowerShell version details. In this case, this is version 7.2.2, although, by the time you read this, the PowerShell team may have created newer versions.
As you can see in step 2, the PowerShell pwsh.json
file contains both the execution policy and a list of modules that PowerShell 7 should never attempt to load (as these three modules are known to not work with PowerShell 7, even using the Windows PowerShell compatibility mechanism). For reasons best known to themselves, the owners of these modules have declined the opportunity to port them over to work with PowerShell 7, although that may change.
In step 4, you view the current PowerShell 7 execution policy. Note that this policy is independent of the Windows PowerShell execution policy.
In step 5, you view the current module folders. This step uses .NET composite formatting and the PowerShell -f
operator. PowerShell provides you with numerous ways to output this information that you may use, including simple variable expansion. Using the method shown in this step can give you more control over the formatting. You can see another example of this type of formatting in step 7.