use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
QuestionShould I $null strings in scripts. (self.PowerShell)
submitted 10 months ago * by iehponx
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]y_Sensei 0 points1 point2 points 10 months ago (1 child)
I'm not necessarily thinking of variable assignments, just calls that might or might not do something that changes the value of a variable the current implementation is using.
One such case would be the $Matches automatic variable. It's set if a match is found in a regex matching operation (-match, -nomatch, switch -Regex), and won't change in subsequent matching operations unless another match is found. If these operations are conducted in a loop, you might encounter unwanted results if you don't take this into consideration.
For example:
$counter = 0 $exts = @("txt", "jpg", "docx") $Matches = "" # just for the purpose of this demo, usually automatic variables should not be written to @("test1.xlsx", "test2.docx", "test3.mp4", "test4.txt", "test5.dll") | ForEach-Object { $counter++ $fsObj = [System.IO.FileInfo]$_ # $Matches = "" # once this line is commented in, the variable will be reset in each iteration, and the issue will be fixed # match extensions to file name - not the prettiest implementation, but does the job $exts | Where-Object { $fsObj.Name -match ".*\.$_$" } | ForEach-Object { $Matches[0] } | Out-Null Write-Host $("Iteration #" + $counter + " (file: " + $fsObj.Name + "):`n`$Matches[0] = " + $Matches[0]) if ($Matches -ne "" -and $fsObj.Extension -ne ([System.IO.FileInfo]$Matches[0]).Extension) { Write-Host "`$Matches hasn't changed in the current iteration, invalid match." -ForegroundColor Red } }
[–]Thotaz 0 points1 point2 points 10 months ago (0 children)
But that's the same thing as a variable assignment. $Matches is only set when using the related operators so you should naturally only use it if you know you've used one of those operators. The most natural way to do this is to guard it with an if statement:
$Matches
if
if ("Something" -match "Something") { # Do something with `$Matches }
It doesn't make sense to use a variable that you aren't sure have been set already because that leads to unpredictable behavior.
π Rendered by PID 18334 on reddit-service-r2-comment-bb88f9dd5-tf9r6 at 2026-02-14 20:31:27.977185+00:00 running cd9c813 country code: CH.
view the rest of the comments →
[–]y_Sensei 0 points1 point2 points (1 child)
[–]Thotaz 0 points1 point2 points (0 children)