0

I'm not sure what I'm missing. This powershell seems to be working the opposite of what I expect. Anyone know why?

$loadUserProfileValue = Get-ItemProperty "IIS:\AppPools\.net v4.5" -Name processModel.loadUserProfile.Value
    Write-Host "Value: $loadUserProfileValue"
    IF ($loadUserProfileValue -eq "False") {
            Write-Host "Since Load User Profile is False, we will now set it to True"}

Here is my Output when Load User Profile is True

Value: True
Since Load User Profile is False, we will now set it to True

Here is my output when Load User Profile is False

Value: False

The value is being picked up correctly. The variable $loadUserProfileValue is correct. The IF Statement is working opposite of what I'm expecting.

I can swap it to be -ne "True" and it seems to work... but why does -eq "False" NOT work?

  • 1
    What is the result of `$loadUserProfileValue.GetType()`? If it is a string, is it possible that there is whitespace (SPACE, TAB, etc.) at the end of the string? – lit Feb 02 '22 at 14:37
  • Lit brings up a good point. If it is a value of `string` type, and not a `boolean`, you can use the `.Trim()` method to get rid of leading, and trailing "white" spaces. – Abraham Zinala Feb 02 '22 at 14:40

2 Answers2

1

You're testing for a string value of false when the property is likely returning a boolean value. PowerShell's type-converter is likely what's responsible for this not throwing an error.

Change your test to use $false instead of 'false' and see if that resolves it. Here is a great article on this:

https://devblogs.microsoft.com/powershell/boolean-values-and-operators/

EDIT: You can (and should) always check a return object's datatype and you can do this with an inherited method on all objects, .gettype(). For your code, it would be: $loadUserProfileValue.gettype() and it will tell you whether the returned object is cast as a boolean, string, etc.

thepip3r
  • 2,855
  • 6
  • 32
  • 38
1

In PowerShell you use the Boolean data type like this: True = $true and False = $false.

In your case you have to change False to $false

$loadUserProfileValue = Get-ItemProperty "IIS:\AppPools\.net v4.5" -Name processModel.loadUserProfile.Value
Write-Host "Value: $loadUserProfileValue"
IF ($loadUserProfileValue -eq $false) {
        Write-Host "Since Load User Profile is False, we will now set it to True"}

There is already a question on that topic on Stack Overflow: Question