You guys are making this too hard. PowerShell handles this quite elegantly e.g.:
Copy> $str1 = $null
> if ($str1) { 'not empty' } else { 'empty' }
empty
> $str2 = ''
> if ($str2) { 'not empty' } else { 'empty' }
empty
> $str3 = ' '
> if ($str3) { 'not empty' } else { 'empty' }
not empty
> $str4 = 'asdf'
> if ($str4) { 'not empty' } else { 'empty' }
not empty
> if ($str1 -and $str2) { 'neither empty' } else { 'one or both empty' }
one or both empty
> if ($str3 -and $str4) { 'neither empty' } else { 'one or both empty' }
neither empty
Answer from Keith Hill on Stack OverflowYou guys are making this too hard. PowerShell handles this quite elegantly e.g.:
Copy> $str1 = $null
> if ($str1) { 'not empty' } else { 'empty' }
empty
> $str2 = ''
> if ($str2) { 'not empty' } else { 'empty' }
empty
> $str3 = ' '
> if ($str3) { 'not empty' } else { 'empty' }
not empty
> $str4 = 'asdf'
> if ($str4) { 'not empty' } else { 'empty' }
not empty
> if ($str1 -and $str2) { 'neither empty' } else { 'one or both empty' }
one or both empty
> if ($str3 -and $str4) { 'neither empty' } else { 'one or both empty' }
neither empty
You can use the IsNullOrEmpty static method:
Copy[string]::IsNullOrEmpty(...)
powershell - Check if a string is not NULL or EMPTY - Stack Overflow
Checking for NULL in PS
PowerShell $null Variable in an IF Statement
how to check a value is that null in powershell script
Videos
if (-not ([string]::IsNullOrEmpty($version)))
{
$request += "/" + $version
}
You can also use ! as an alternative to -not.
You don't necessarily have to use the [string]:: prefix. This works in the same way:
if ($version)
{
$request += "/" + $version
}
A variable that is null or empty string evaluates to false.
The problem is here:
$UserPref = if ($UserPref -eq $null){$UserPrefN = $UserGN} Else{$UserPrefN = $UserPref}
You’re doing a comparison on the $UserPref variable which has not been set because the comparison is in the assignment statement. This is not technically illegal, but you have strict mode on which will throw an error when you reference a variable that hasn’t been initiated. You can avoid this error by skipping the Set-StrictMode cmdlet.
However, the way you are assigning the $UserPref variable seems a bit wonky to me. It looks like you’re trying to check if it’s already set and then set some other variable values based on this comparison. Since each action you’re taking in the if construct is an assignment, there is no need to encapsulate those in the $UserPref assignment. Also, instead of checking for -eq $null in the if condition, you could just check for the existence of $UserPref:
if (!$UserPref){$UserPref = $UserGN}
I know I’m going to be kicking myself, but since I still consider myself a n00b with PS, I’ll try not to kick too hard.
I’ve got a script for new hires that works intermittently. HR insists that AD accounts use a person’s legal name, however many people prefer their nicknames for their email and display names, so a PreferredName field was created in HRIS. I’m checking for a preferred name ($UserPref) and if it exists I assign it to $UserPrefN and if it doesn’t, I assign their first name ($UserGN) to $UserPrefN. If $UserPref is blank ($null), sometimes it works and sometimes it doesn’t, lately more often than not it doesn’t and $UserPrefN doesn’t get set. It’s got to be my syntax, but I don’t see it.
[PS] C:\Windows\system32>Set-Strictmode -Version Latest -Verbose
[PS] C:\Windows\system32>$User = 'AccountName'
[PS] C:\Windows\system32>$UserSN = 'LastName'
[PS] C:\Windows\system32>$UserGN = 'FirstName'
[PS] C:\Windows\system32>$UserFI = $UserGN.substring(0,1)
[PS] C:\Windows\system32>$UserPref =
>> if ($UserPref -eq $null){$UserPrefN = $UserGN} Else{$UserPrefN = $UserPref}
>>
The variable '$UserPref' cannot be retrieved because it has not been set.
At line:2 char:5
+ if ($UserPref -eq $null){$UserPrefN = $UserGN} Else{$UserPrefN = $UserPref}
+ ~~~~~~~~~
+ CategoryInfo : InvalidOperation: (UserPref:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
[PS] C:\Windows\system32>
[PS] C:\Windows\system32>echo $UserPrefN
The variable '$UserPrefN' cannot be retrieved because it has not been set.
At line:1 char:6
+ echo $UserPrefN
+ ~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (UserPrefN:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
I'm troubleshooting a rebuild of my company's server build script and I'm trying to build logic into the script that acts based on whether a variable is null. We build different types of servers and I want the script to essentially create commands to be run later on in the script execution, but I don't want those commands to be created or run if the value of the variable given is null. Here's what I'm looking at (pardon this will be psuedo code and likely a bit sloppy:
$VMIP2 = Read-Host "Enter IP for $VM (x.x.x.x format)" if ($VMIP2 -eq $null) Do nothing if ($VMIP2 -eq "Any valid IP address") Create the following command as a variable: $NewCommand = "New-NetIPAddress -InterfaceAlias "IntAlias" -IPAddress <IPAddress> -PrefixLength 24 -SkipAsSource $true" else Write-Verbose "Input not valid, please re-enter $VMIP2"
I also want to do this with my $Domain variable, but instead creating the Add-Computer command. I have some users who are very Powershell illiterate and I'm trying to make the script as user-friendly as possible. Any and all help is always appreciated, thanks.
If(!$Variable) vs if($Variable -eq $null)
Is there much difference or clear advantages between the two?