2

Simple Powershell script:

$key = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager"
$bootexecute = $key.BootExecute

write-host $bootexecute

if ($bootexecute -eq "autocheck autochk *") {
    return $false
} else {
    return $true
}

This is What I'm getting as output:

autocheck autochk /r \??\C: autocheck autochk *
False

So even though the $bootexecute variable does not exactly equals "autocheck autochk *" I still get "False" as a result, whereas it should return "True" here.

What's going on, what am I missing here?

Edit, to clarify: I literally want to check for the string "autocheck autochk *". Asterisk included.

5
  • what do you get with: $bootexecute | Get-Member? Commented Jun 29, 2016 at 13:42
  • 1
    $bootexecute is being returned as an string array, BTW. Commented Jun 29, 2016 at 13:48
  • @tire0011: TypeName: System.String Commented Jun 29, 2016 at 13:50
  • @TonyHinkle: You were right, it was an array. If I change $bootexecute to $bootexecute[0], it works exactly as intended. Could you put that as an answer so I can accept it? Commented Jun 29, 2016 at 13:59
  • nvm. Im an idiot..... I piped it into GM and forgot to use inputobject. It is a string array. I have a better solution though than what was suggested here. Commented Jun 29, 2016 at 14:10

3 Answers 3

2

Since $bootexecute is evaluating to:

autocheck autochk /r \??\C: autocheck autochk *

-eq is probably not what you want.

Instead, use a regular expression and -match:

if ($bootexecute -match "autocheck autochk .*") {
    return $false
} else {
    return $true
}

which can just be simplified to:

$bootexecute -match "autocheck autochk .*"
return
Sign up to request clarification or add additional context in comments.

Comments

2

Your -eq condition is not what you want.

You are doing a equal comparison on a long string with a partial match that will always be false. You might actually want a wildcard like.

$bootexecute -like "autocheck autochk *"

That should get what you want. However if you are trying to match the string literal then -like would not be good. I had assumed incorrectly that you were using a wildcard.

The point is that -eq would not work as there is more to the string then just "autocheck autochk *".

Consider the following to show why the -eq was "failing"

"autocheck autochk *" -eq "autocheck autochk *"
True

"autocheck autochk /r \??\C: autocheck autochk *" -eq "autocheck autochk *"
False

Working with arrays

As pointed out by Tony in comments you are getting an array returned. When treated as a string PowerShell concats it with spaces which is why write-host is displaying what it was. Since you know the full element you are testing for the array operator -contains makes more sense here.

$bootexecute -contains "autocheck autochk *"

Looking at the registry you see that BootExecute is a REG_MULTI_SZ so getting an array would be expected.

3 Comments

>The asterisks is being interpreted literally Well, that's what I want because it's literally there.
No. The whole string is being compared. Your string contains other data as well. Are you looking for a match at the end of the string?
Oh... I am right about the condition you are using being incorrect. I got the one you needed wrong it seems as you are getting an array returned.
1

A string array is being returned from get-itemproperty, so $bootexecute is an array. The if statement evaluates to true because one of the items in the array is equal to the specified string.

If you want to compare just the first item in the array, you can change the if statement to:

if ($bootexecute[0] -eq "autocheck autochk *")

If you're wanting to compare all of them (which is what the posted code is doing), you could use .contains to make the code clearer:

if ($bootexecute.contains("autocheck autochk *"))

1 Comment

Shouldn't matter in this case but im pretty sure that the .contains() method is case sensitive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.