Tuesday, July 4, 2023
Docker/Windows: Installation Requirement "BIOS-level Hardware Virtualization"
Wednesday, June 7, 2023
PowerShell: Installing the latest version of Pester
Before installing the latest version of Pester, uninstall the legacy version of Pester (Pester 3.x) which is installed with most modern versions of Windows (see PowerShell: Uninstalling Pester 3.0). On a machine with PowerShell 5.0 or later install, the latest version of Pester can be installed as follows without running as administrator:
Once install verify that version of Pester is the most recent:
(Get-Module -ListAvailable Pester).Version
An example of installing Pester is as follows (not that a user is prompted to accept the modules being installed):
E:\Users\Jann\PowerShellRepos> Install-Module -Name Pester
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository
cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): A
Tuesday, June 6, 2023
PowerShell: Uninstalling Pester 3.0
Microsoft by default installs an obsolete version of Pester on Windows (Pester 3.x). For example on one of my machines (Windows 11) the version of Pester installed is 3.4.0 (2016 Pester). In fact, Pester 3.4.0 is installed for x64 and x86 (32-bit) versions of PowerShell. To uninstall Pester 3.4.0 means that the x64 and x86 installs have to be uninstalled.
Detecting the Available Versions of Pester Installed
The following PowerShell displays available versions of Pester installed:
The default version of Pester installed on my Windows 11 host is as follows:
Uninstalling Pester 3.x
Monday, June 5, 2023
PowerShell: Requiring a Script to Run as Administrator
Placing the following at the top of a PowerShell script requires that said script to run as administrator:
#Requires -RunAsAdministrator
Sunday, May 28, 2023
PowerShell: StringBuild AppendLine lessons from C
Two years ago I wrote a post, PowerShell: Inadvertently Returning Multiple Values from a Function and low and behold I found a found a common C# data type that is a common culprit of this issue, StringBuilder. I have coded C# for twenty-tree years and I did not realize the each Append* method of StringBuilder returns a reference to the StringBuilder.
To demonstrate consider this C# snippet:
In the documentation for the AppendLine method, AppendLine(String), the return value of AppendLine and each Append* method of StringBuilder is defined as follows:
A clearer way to write the above code in C# would be acknowledge the return value and to ignore it:
The following code shows PowerShell invoking AppendLine multiple times:
Although it appears that the PowerShell function, Get-EnvironmentProperties, returns a string. Result (the return value from Get-EnvironmentProperties) in an array of 10 elements:
The method AppendLine is invoked nine times so the first nine elements of the array. The tenth element of the array (index of 9) is the string return in the last line of function, Get-EnvironmentProperties.
Suppressing the StringBuilder returned by AppendLine results in the the correct behavior, the lone return value is as string as is show below:
A variety of mechanism were show to suppress return value of AppendLine. From the performance stand point, Out-Null is the slowest but from a readability stand point, it is the most readable for all levels of PowerShell developer.
In my code I used the following approach as I learned C as my first programming language:
With regard to performance and suppressing the result of a method/expression StackOverflow has an excellent post on the topic What's the better (cleaner) way to ignore output in PowerShell? A response by JasonMArcher demonstrates and Out-Null has the worst performance.
Monday, May 8, 2023
Visual Studio Code: Disable Format on Save per-File (including wildcards)
In this post, we'll explore how to disable the formatOnSave option for specific files, multiple files, using wild cards, and files with certain extensions.
Disabling formatOnSave for a specific file
To disable formatOnSave for a specific file, you can add the following setting to your settings.json file:
"editor.formatOnSave": false
}
Replace file path/filename.ext (noted in boldface) with the path and filename of the file for which you want to disable formatOnSave.
Disabling formatOnSave for multiple files
To disable formatOnSave for multiple files, you can add the following setting to your settings.json file:
"[file path/filename1.ext]": {
"editor.formatOnSave": false
},
"[file path/filename2.ext]": {
"editor.formatOnSave": false
}
Replace file path/filename1.ext and file path/filename2.ext (noted in boldface) with the path and filenames of the files for which you want to disable formatOnSave.
Disabling formatOnSave using wildcards
You can also disable formatOnSave for files that match a specific pattern using wildcards. For example, to disable formatOnSave for files that have a specific prefix, you can add the following setting to your settings.json file:
"[prefix]*.ext": {
"editor.formatOnSave": false
}
Replace prefix (noted in boldface) with the desired prefix for the files you want to exclude from formatOnSave.
Similarly, to disable formatOnSave for files that have multiple possible extensions, you can use a wildcard to match the extensions. For example:
"[file path/*.ext1, *.ext2]": {
"editor.formatOnSave": false
}
Replace file path with the path to the directory containing the files you want to exclude from formatOnSave. Replace ext1 and ext2 with the extensions of the files you want to exclude from formatOnSave.
Conclusion
And that's it! With these settings, you can easily disable the formatOnSave option for specific files, multiple files, or using wildcards.









