-
Notifications
You must be signed in to change notification settings - Fork 143
Basic PowerShell Tricks and Notes Part 4
The following PowerShell series is designed for newcomers to PowerShell who want to quickly learn the essential basics, the most frequently used syntaxes, elements and tricks. It can also be used by advanced users as a quick reference or those who want to sharpen their skills.
The main source for learning PowerShell is Microsoft Learn websites. There are extensive and complete guides about each command/cmdlet with examples.
PowerShell core at Microsoft Learn
You can also use the Windows Copilot for asking any PowerShell related questions, code examples etc.
This is part 4 of this series, find other parts here:
When using Set-Variable cmdlet to create a constant variable, you can't change the value of that variable later in the script by simple assignments, but there is a way to bypass this limitation using reflection.
Set-Variable -Name 'MyConstantVariable' -Value 'Hello World' -Option 'Constant'
$MyConstantVariable
$PSVar = Get-Variable -Name 'MyConstantVariable'
$PSVar.GetType().GetField('_value', [System.Reflection.BindingFlags] 'NonPublic, Instance').SetValue($PSVar, 'Wut')
$MyConstantVariable
$PSVar.GetType().GetField('_options', [System.Reflection.BindingFlags] 'NonPublic, Instance').SetValue($PSVar, [System.Management.Automation.ScopedItemOptions]::None)
$MyConstantVariable = 'Lolz'
$MyConstantVariable
Shout out to Santiago Squarzon for this trick.
The way you can prevent this bypass is by defining constant variables in C# code in PowerShell. The reflection method demonstrated above won't work on this type of constant variables.
Add-Type -TypeDefinition @'
namespace NS
{
public static class Const
{
public const int myConst = 66;
}
}
'@ -Language CSharp
([NS.Const]::myConst)
In PowerShell, the presence of the following commands in a ScriptBlock will completely disable optimizations in that ScriptBlock:
New-Variable
Remove-Variable
Set-Variable
Set-PSBreakpoint
# Also Dot-Sourcing
. .\file.ps1
# Also if any type of breakpoint is already set
Also usage of any AllScope variable in a ScriptBlock will disable optimization in there.
You can view those commands in here too.
Shout out to SeeminglyScience for this info.
Any PowerShell code at some point will run and be in a ScriptBlock. Functions are their own ScriptBlock, modules (.psm1 files) are their own ScriptBlock, script files (.ps1 files) are their own ScriptBlock, ScriptBlocks themselves are their own ScriptBlock and so on.
So the presence of the above methods and commands inside a ScriptBlock in the context explained above will disable optimization in that ScriptBlock, you can however use them outside of the ScriptBlock and then utilize them inside which will not disable optimization in that ScriptBlock.
This example will enumerate all files in an array of directories based on specific extensions and return an array of FileInfo objects. This is potentially the fastest and most optimized way to do this. This command takes ~9 seconds to complete on my system.
# Define a HashSet of file extensions to filter by
$Extensions = [System.Collections.Generic.HashSet[System.String]]@('.sys', '.exe', '.com', '.dll', '.rll', '.ocx', '.msp', '.mst', '.msi', '.js', '.vbs', '.ps1', '.appx', '.bin', '.bat', '.hxs', '.mui', '.lex', '.mof')
# Define an array of directory paths to scan through
[System.IO.DirectoryInfo[]]$Paths = 'C:\ProgramData\Microsoft', 'C:\Program Files\Windows Defender', 'C:\Program Files\Hyper-V'
# Define a HashSet to store the initial output
$Output = [System.Collections.Generic.HashSet[System.IO.FileInfo]]@()
# Define the GetFiles parameters
$Options = [System.IO.EnumerationOptions]@{
IgnoreInaccessible = $true
# This is equal to -Recurse parameter in Get-ChildItem cmdlet
RecurseSubdirectories = $true
# By default is skips hidden and system files, here we just skip the System files
# https://learn.microsoft.com/en-us/dotnet/api/system.io.fileattributes
# https://learn.microsoft.com/en-us/dotnet/api/system.io.enumerationoptions.attributestoskip
AttributesToSkip = 'System'
}
# Loop over each path and add the files to the output HashSet using UnionWith
foreach ($Path in $Paths) {
$Output.UnionWith((Get-Item -LiteralPath $Path).GetFiles('*', $Options))
}
# Define a HashSet to store the filtered output - Making sure the comparison is case-insensitive since "Get-ChildItem -Include" is case-insensitive as well and we don't want to miss files with ".DLL" extension and so on
$OutputAfterFiltering = [System.Collections.Generic.HashSet[System.IO.FileInfo]]@( $Output.Where({ $Extensions.Contains($_.Extension.ToLower()) }))
This is an improved variation of the script above that handles inaccessible directories better but takes a few seconds (~3) more to complete.
# Define a HashSet of file extensions to filter by
$Extensions = [System.Collections.Generic.HashSet[System.String]]::new(
[System.String[]] ('.sys', '.exe', '.com', '.dll', '.rll', '.ocx', '.msp', '.mst', '.msi', '.js', '.vbs', '.ps1', '.appx', '.bin', '.bat', '.hxs', '.mui', '.lex', '.mof'),
# Make it case-insensitive
[System.StringComparer]::InvariantCultureIgnoreCase
)
# Define an array of directory paths to scan through
[System.IO.DirectoryInfo[]]$Paths = 'C:\ProgramData\Microsoft', 'C:\Program Files\Windows Defender', 'C:\Program Files\Hyper-V'
# Define a HashSet to store the initial output
$Output = [System.Collections.Generic.HashSet[System.IO.FileInfo]]@()
# Define the GetFiles parameters
$Options = [System.IO.EnumerationOptions]@{
IgnoreInaccessible = $true
# This is equal to -Recurse parameter in Get-ChildItem cmdlet
RecurseSubdirectories = $true
# This is equal to -Force parameter in Get-ChildItem cmdlet
AttributesToSkip = 'None'
}
$Output = foreach ($Path in $Paths) {
[System.IO.Enumeration.FileSystemEnumerator[System.IO.FileInfo]]$Enum = $Path.EnumerateFiles('*', $Options).GetEnumerator()
while ($true) {
try {
# Move to the next file
if (-not $Enum.MoveNext()) {
# If we reach the end of the enumeration, we break out of the loop
break
}
# Check if the file extension is in the Extensions HashSet
if ($Extensions.Contains($Enum.Current.Extension)) {
# Pass the file to the output
$Enum.Current
}
}
catch {}
}
}
Shout out to Santiago Squarzon for providing this method.
For comparison, the following command takes ~20 minutes to complete on my system and produces the same exact output as the scripts above but it's 100x times slower.
[System.IO.DirectoryInfo[]]$Paths = 'C:\ProgramData\Microsoft', 'C:\Program Files\Windows Defender', 'C:\Program Files\Hyper-V'
[System.String[]]$Extensions = @('*.sys', '*.exe', '*.com', '*.dll', '*.rll', '*.ocx', '*.msp', '*.mst', '*.msi', '*.js', '*.vbs', '*.ps1', '*.appx', '*.bin', '*.bat', '*.hxs', '*.mui', '*.lex', '*.mof')
[System.IO.FileInfo[]]$Output = Get-ChildItem -Recurse -File -LiteralPath $Paths -Include $Extensions -Force -ErrorAction SilentlyContinue
[System.AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object -FilterScript { $_.Location } |
Sort-Object -Property FullName |
Select-Object -Property FullName, Location, GlobalAssemblyCache, IsFullyTrusted |
Out-GridView -OutputMode Multiple
foreach ($Dll in (Convert-Path -Path ("$([psobject].Assembly.Location)\..\*.dll"))) {
try {
Add-Type -Path $Dll
}
catch {}
}
foreach ($Dll in Get-ChildItem -File -Filter '*.dll' -Path $PSHOME) {
try {
Add-Type -AssemblyName ($Dll.Name).Replace($Dll.Extension, '')
}
catch {}
}
Utilizing a try/catch block is essential in this scenario where not all DLLs located at the root of PowerShell are importable. This code is compatible with PowerShell installed through the Store or the MSI file.
Manually importing DLLs can be advantageous, particularly when transitioning from writing code in VS Code to executing it in PowerShell within Windows Terminal. In VS Code, the integrated extension automatically loads necessary assemblies, ensuring smooth operation. However, this automatic process is absent outside of VS Code, potentially leading to discrepancies in code behavior. To mitigate this, manually identifying and importing the missing assemblies is required. By importing all DLLs preemptively, you eliminate the concern of missing assemblies.
To streamline this process, incorporate that code into your RootModule.psm1
or ScriptsToProcess.ps1
file. This ensures the code executes solely during the module's import phase, optimizing performance.
Note
The -ReferencedAssemblies
in Add-Type -Path <Paths> -ReferencedAssemblies <Assemblies>
uses lazy loading, if a referenced assembly is not part of the type being added or the type doesn't reference a member from that assembly, nothing really happens. This lazy loading behavior improves load times.
Shout out to SeeminglyScience for providing this info.
If you don't need the extra features of PSCustomObjects such as dynamically adding/removing properties then consider defining and using custom classes in C#
and use them in PowerShell. They are at least twice faster and this is very visible in loops. Run the benchmark below to see the difference.
# Define a C# class in PowerShell
Add-Type -TypeDefinition @'
public class CustomCSharpClass {
public string Property { get; set; }
public CustomCSharpClass(string property) {
Property = property;
}
}
'@
# Benchmark PSCustomObject creation
$psCustomObjectTime = Measure-Command {
for ($i = 0; $i -lt 10000; $i++) {
$obj = [PSCustomObject]@{Property = 'Value' }
}
}
# Benchmark C# class object creation
$csharpClassTime = Measure-Command {
for ($i = 0; $i -lt 10000; $i++) {
$obj = [CustomCSharpClass]::new(
'Value'
)
}
}
# Output the results
"PSCustomObject creation time: $($psCustomObjectTime.TotalMilliseconds) ms"
"C# class object creation time: $($csharpClassTime.TotalMilliseconds) ms"
-
+=
operator -
Where-Object
cmdlet -
ForEach-Object
cmdlet
Instead, use Foreach
loop with Direct Assignment. It's a language construct, it's always faster than the cmdlets and has less overhead.
Here is a small benchmark you can run to see the difference in timing
# Define your collection
$Y = 1..10000
# First script
$firstScript = {
$array = foreach ($X in $Y) {
$X
}
}
# Second script
$secondScript = {
$array = @()
foreach ($X in $Y) {
$array += $X
}
}
# Third script
$thirdScript = {
$array = New-Object -TypeName 'System.Collections.Generic.List[psobject]'
foreach ($X in $Y) {
$array.Add($X)
}
}
# Measure the time taken by the first script
$firstScriptTime = Measure-Command -Expression $firstScript
# Measure the time taken by the second script
$secondScriptTime = Measure-Command -Expression $secondScript
# Measure the time taken by the third script
$thirdScriptTime = Measure-Command -Expression $thirdScript
# Output the results
"First script execution time: $($firstScriptTime.TotalMilliseconds) ms"
"Second script execution time: $($secondScriptTime.TotalMilliseconds) ms"
"Third script execution time: $($thirdScriptTime.TotalMilliseconds) ms"
- App Control for Lightly Managed Devices
- App Control for Fully managed device - Variant 1
- App Control for Fully managed device - Variant 2
- App Control for Fully managed device - Variant 3
- App Control for Fully managed device - Variant 4
- App Control Notes
- How to Create and Deploy a Signed App Control Policy
- Fast and Automatic Microsoft Recommended Driver Block Rules updates
- App Control policy for BYOVD Kernel mode only protection
- EKUs in App Control for Business Policies
- App Control Rule Levels Comparison and Guide
- Script Enforcement and PowerShell Constrained Language Mode in App Control Policies
- How to Use Microsoft Defender for Endpoint Advanced Hunting With App Control
- App Control Frequently Asked Questions (FAQs)
- New-SupplementalWDACConfig
- Remove-WDACConfig
- Edit-WDACConfig
- Edit-SignedWDACConfig
- Deploy-SignedWDACConfig
- Confirm-WDACConfig
- New-DenyWDACConfig
- Set-CommonWDACConfig
- New-KernelModeWDACConfig
- Get-CommonWDACConfig
- Remove-CommonWDACConfig
- Assert-WDACConfigIntegrity
- Build-WDACCertificate
- Test-CiPolicy
- Get-CiFileHashes
- Get-CIPolicySetting
- Create Bootable USB flash drive with no 3rd party tools
- Event Viewer
- Group Policy
- How to compact your OS and free up extra space
- Hyper V
- Overrides for Microsoft Security Baseline
- Git GitHub Desktop and Mandatory ASLR
- Signed and Verified commits with GitHub desktop
- About TLS, DNS, Encryption and OPSEC concepts
- Things to do when clean installing Windows
- Comparison of security benchmarks
- BitLocker, TPM and Pluton | What Are They and How Do They Work
- How to Detect Changes in User and Local Machine Certificate Stores in Real Time Using PowerShell
- Cloning Personal and Enterprise Repositories Using GitHub Desktop
- Only a Small Portion of The Windows OS Security Apparatus
- Rethinking Trust: Advanced Security Measures for High‐Stakes Systems
- Clean Source principle, Azure and Privileged Access Workstations
- How to Securely Connect to Azure VMs and Use RDP
- Basic PowerShell tricks and notes
- Basic PowerShell tricks and notes Part 2
- Basic PowerShell tricks and notes Part 3
- Basic PowerShell tricks and notes Part 4
- Basic PowerShell tricks and notes Part 5
- How To Access All Stream Outputs From Thread Jobs In PowerShell In Real Time
- PowerShell Best Practices To Follow When Coding
- How To Asynchronously Access All Stream Outputs From Background Jobs In PowerShell
- Powershell Dynamic Parameters and How to Add Them to the Get‐Help Syntax
- RunSpaces In PowerShell
- How To Use Reflection And Prevent Using Internal & Private C# Methods in PowerShell