1

I'm trying to set the system folder path in the $SystemFolder array. However, the code gives error.

$Arch = (Get-WmiObject -Class Win32_OperatingSystem).OSArchitecture

If ($Arch -eq "64-bit") {
    $SystemFolder = "$env:SystemRoot\SysWOW64"
}

ElseIf ($Arch -eq "32-bit") {
    $SystemFolder = "$env:SystemRoot\System32"
}

The array $SystemFolder remains $null!

I suspect the error is related to the idea that I put an array inside a conditional if statement. (I'm not sure)

I am confused, because the code is very simple, and it looks correct.

I really appreciate anyone trying to help me.

Se7en
  • 363
  • 4
  • 13
  • 1
    What is the error? and if $SystemFolder is $null, then your conditional didn't apply. You can either add an 'Else' clause or examine what your $Arch variable actually returns? I suspect, based on the code, that it's neither of those string values. – thepip3r Feb 24 '22 at 21:13
  • 4
    I believe this could be reduced to `if([environment]::Is64BitOperatingSystem){ ... }` – Santiago Squarzon Feb 24 '22 at 21:16
  • 4
    Note that the `.OSArchitecture` value may be _localized_; e.g., with the Dutch culture in effect, you'd get `64 bits`. Also, as an aside, the obligatory reminder: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Feb 24 '22 at 21:26
  • 2
    Some system culture settings return `64 bits` instead of `64-bit`... Best add `-replace '\D'` to remove all non numeric characters and simply test for either `64` or `32` – Theo Feb 24 '22 at 21:28
  • 2
    See [this answer](https://stackoverflow.com/a/70893061/9898643) – Theo Feb 24 '22 at 21:55
  • 3
    Use `[environment]::Is64BitOperatingSystem)` as per Santiago's comment for the reasons mentionned by Theo / mklment0. To answer your other interrogation, this is not a scoping issue. Even though you declare the variable in the If block, if assigned, it will remain valid outside of it. In your case, it simply do not enter any of the If condition. I recommend you to check a Powershell debugging tutorial. By debugging, you could have seen right away what was the issue by looking at the variable. I guarantee it will pay up for the time you'll need to learn how to do it. – Sage Pourpre Feb 24 '22 at 22:37
  • 3
    All that being said, in this case, you don't even need debugging. Just put `Write-Host $Arch` right under your first line and you'll see right away the value of it. It is probably written differently than what you expect and thus do not enter one of your 2 `If` conditions. – Sage Pourpre Feb 24 '22 at 22:38
  • 1
    Thanks friends for the help! The issue was that ```OSArchitecture``` value return ```64 bits``` instead of ```64-bit``` (as @mklement0 & @Theo noted), and this was the opposite of what was expected in the if condition. FIXED! – Se7en Feb 25 '22 at 07:23
  • 1
    I switched the code to ```if([environment]::Is64BitOperatingSystem){ ... }``` (Thanks to @santiago-squarzon & @sage-pourpre ), in order to avoid ```Get-WmiObject``` as @mklement0 noted. – Se7en Feb 25 '22 at 07:24
  • So now, what is more reliable to use: ```Get-CimInStance Win32_OperatingSystem).OSArchitecture```, or ```[environment]::Is64BitOperatingSystem)```? – Se7en Feb 25 '22 at 09:18
  • 1
    @Se7en see Theo's linked answer which handles all possibilities – Santiago Squarzon Feb 25 '22 at 12:29

0 Answers0