r/PowerShell • u/Ummgh23 • Mar 31 '26
Question Best Pracise - Leave a variable created by a conditional check uninitialized or set it to $null?
Just wondering what everyone's opinion on this is. Do you prefer to null a variable or just not initialize it? I couldn't find any "official" best practise for this.
PowerShell does evaluate uninitialized variables to $null automatically, but manually assigning it might help with clarity and possibly some edge cases where not initializing it could cause an issue?
For example, if a variable exists in the caller's scope. Not nulling it would carry its value over into the current scope.
As an example:
if ($SomeCondition) {
$SomeVariable = "Condition is true"
} else {
$SomeVariable = $null
}vs.
if ($SomeCondition) {
$SomeVariable = "Condition is true"
}
18
Upvotes
11
u/BlackV Mar 31 '26 edited Mar 31 '26
alternately
there is not a perfect answer, it really depends on what you are doing inside you
if(or loop or whatever)But this
is not good practice (generally)
but pick a style and stick with it, constancy is best
6
u/cottonycloud Mar 31 '26
I prefer below or some type of null operators depending on the conditional.
8
u/Thotaz Mar 31 '26
Because of how PowerShell inherits the values from parent scopes I think you should always make sure you've assigned a value in the current scope before trying to use it. You can see it in action here:
In this case
$Var1inside the function will have "Test" as the value because it was set in a parent scope. In your case, rather than doing the assignment inside the if statement, you can assign the if statement itself:This saves you the trouble of writing the
elsepart.1
u/Numerous-Pickle-5850 Mar 31 '26
Depends if you (plan to) loop back/refer to it later, otherwise just don't.
3
u/TheGooOnTheFloor Mar 31 '26
As an aside, you're mixing types. If a later step is expecting a string, assigning $null to the variable could introduce unexpected results. I prefer to use
That way there is consistency in the type.
3
u/OPconfused Mar 31 '26
Imo, most elegant is
This will automatically set
$someVariableto$nullif$SomeConditionfails. It won't matter what it was defined to in the caller's scope; it will now be reset in the scope of the function you're using.In general, it's good to take advantage of powershell's ability of direct variable assignment via expressions.
2
u/pigers1986 Mar 31 '26
i prefer clear state , so $null
2
u/ankokudaishogun Mar 31 '26
Scope shenanigans aside, I think you are looking at this the wrong way.
The question is not "What if
$SomeVariableis not initialized?" but "What if$SomeVariablehas the value I'm looking for?"You don't really need to care about
$SomeVariableunless it has the value you are looking for.Therefore, do not bother to actively setting a variable to
$nullunless you are "cleaning up" a variable to recycle it within the scope(i.e.: in a loop)1
u/BladeLiger Mar 31 '26
Powershell supports scoped variables.
or
will exclude any parent scope variables you have. Though I am not sure about the differences between function or local.
1
u/dodexahedron Mar 31 '26
So why not scope it explicitly?
Get-Help about_ScopesUse
$private:fooif you don't want that variable to live past the current scope.1
u/[deleted] Apr 01 '26
I only set a variable to $null if I am planning on using the variable again.
Like looping through a list.
Example: I have a script that sends emails to users when their account is 2 weeks away from a password reset.
It emails the service desk if the account is a service account.
My logging sets True/False flags for if the user or service desk have been emailed.
I set these to $null at the begining of the loop.
1
u/C0gn171v3D1550n4nc3 Apr 04 '26
If it's a boolean, set to false rather than $null, surely, perhaps I'm missing the point.
0
u/node77 Mar 31 '26
Im not sure in theory there are any differences. I have did it both ways. Global variables, local function variables, as in C sharp. I don’t think PowerShell distinguishes between the two, but to be proper readable code, set it to null. But then you have to use it in some cases. I could never find a best practice article by Microsoft either.