Search code examples
powershellpowershell-3.0windows-server-2012

Test-Path issue


Why does Test-Path -Path $folder -PathType Container return true, but folder does not exist? The Path is:

C:\Windows\system32\WindowsPowerShell\v1.0\Modules\MyCompany\Modules

Same code in ISE x86 and ISE, but different results

enter image description here

enter image description here


Solution

  • The answer is here

    Excerpt:

    The 'System32' folder is for 64-bit files and the 'SysWOW64' folder is for 32-bit files

    This can be somewhat confusing, but the System32 folder is intended for 64-bit files and the SysWOW64 folder is intended for 32-bit files.

    This may seem a bit illogical if you look at the folder names, but there is an explanation to this. It has to do with compatibility. Many developers have hard coded the path to the system folder in their applications source code. They have included "System32" in the folder path. And to preserve compatibility, if the application is converted to 64-bit code, the 64-bit system folder is still named System32.

    But what about 32-bit applications that have the system path hard coded and is running in a 64-bit Windows? How can they find the new SysWOW64 folder without changes in the program code, you might think.

    The answer is that the emulator redirects calls to System32 folder to the SysWOW64 folder transparently so even if the folder is hard coded to the System32 folder (like C:\Windows\System32), the emulator will make sure that the SysWOW64 folder is used instead. So same source code, that contains a path with the System32 folder included, can be compiled to both 32-bit and 64-bit program code without any changes.

    So remember: • the SysWOW64 folder is intended for 32-bit files only • the System32 folder is intended for 64-bit files only It is very important that a binary file compiled to a specific bitness (32 or 64) is installed to the correct system folder. Otherwise the program that needs the file will not be able to load the file and will probably not work as expected.

    That's why ISE x86 output is in SysWow64 folder and the call to:

    C:\Windows\system32\WindowsPowerShell\v1.0\Modules\MyCompany\Modules

    is redirected to the SysWow64 folder.

    The ISE console is running as x64 and therefore will look in System 32 and will not find this folder, but the folder was created in ISE x86 console, which saves the folder under SysWOW64.