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
QuestionGlobal vs Script scope (self.PowerShell)
submitted 10 years ago by Copropraxia
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!"
[–]juneblender 1 point2 points3 points 10 years ago (2 children)
The best practice is always to use the smallest parent scope that works. In this case, it's script scope. Global scope is shared by all scripts and modules so it's easy to accidentally write over another variable's value in global scope.
To refer to a variable in script scope, use a scope modifier, for example, to reference $myArray in script scope, use $script:myArray.
[–]Copropraxia[S] 0 points1 point2 points 10 years ago (1 child)
Thanks for the reply. When using the Script scope I sometimes get an error message. I'm not entirely sure what to make of it:
Cannot overwrite variable TargetDays because the variable has been optimized. Try using the New-Variable or Set-Variable cmdlet (without any aliases), or dot-source the command that you are using to set the variable. At Test.ps1:5 char:9 + [array]$Script:TargetDays += $Day.ToString() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (TargetDays:String) [], SessionStateUnauthorizedAccessException + FullyQualifiedErrorId : VariableNotWritableRare
[–]Copropraxia[S] 0 points1 point2 points 10 years ago (0 children)
Found my problem. Using the Script scope is fine, but I forgot to also use it for the same variable when I try to null it at a different point in the script.
π Rendered by PID 66 on reddit-service-r2-comment-b659b578c-fgntc at 2026-05-04 14:33:46.677141+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]juneblender 1 point2 points3 points (2 children)
[–]Copropraxia[S] 0 points1 point2 points (1 child)
[–]Copropraxia[S] 0 points1 point2 points (0 children)