{"id":10560,"date":"2021-12-13T04:33:04","date_gmt":"2021-12-13T12:33:04","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=10560"},"modified":"2022-02-21T13:43:23","modified_gmt":"2022-02-21T21:43:23","slug":"running-powershell","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/running-powershell\/","title":{"rendered":"Q&#038;A: Why does AlwaysUp run the 32-bit Version of PowerShell?"},"content":{"rendered":"<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"\/blog\/images\/qa-alwaysup-run-powershell-64-bit.webp\" style=\"margin-bottom:20px;\" title=\"Why does AlwaysUp run the 32-bit Version of PowerShell?\" alt=\"Why does AlwaysUp run the 32-bit Version of PowerShell?\" border=\"0\" width=\"380\" height=\"160\"><\/div>\n<div class=\"blog-qa-question-box\">\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.coretechnologies.com\/images\/quotes-transparent-21x21.png\" width=\"21\" height=\"21\">&nbsp;&nbsp;We have been trying to get a particular <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/overview?view=powershell-7.1\" target=\"_blank\" rel=\"noopener\">PowerShell script<\/a> running as a service for some time now. I have been pulling my hair out but I think I finally found the issue and I don&#8217;t understand why it&#8217;s happening.<\/p>\n<p>I am using a 64-bit PowerShell module (SqlServer). My script runs fine in the <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/windows-powershell\/ise\/introducing-the-windows-powershell-ise?view=powershell-7.1\" target=\"_blank\" rel=\"noopener\">ISE<\/a>, no issues.  But, every time I run it from AlwaysUp it gets an error that it cannot find the module. What I finally found out is AlwaysUp is launching the 32-bit version of PowerShell and not the 64-bit version that I\u2019m calling out.  So, even though my script calls the 64-bit version of PowerShell, it&#8217;s launching the 32-bit version, which is not compatible with what I&#8217;m doing.<\/p>\n<p>Here is what I have specified in AlwaysUp:<\/p>\n<p>Application: <b><span class=\"break-long-words\">C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe<\/span><\/b><br \/>\nArguments: <b>&lt;The full path to my PowerShell script&gt;<\/b><br \/>\nStart in: <b>&lt;The folder containing my PowerShell script&gt;<\/b><\/p>\n<p>How I\u2019m confirming my suspicions:<\/p>\n<p>I&#8217;m running the PS command <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.environment.is64bitprocess?view=net-5.0\" target=\"_blank\" rel=\"noopener\">[Environment]::Is64BitProcess<\/a> and dumping the display to a text file. When running this command, it returns true if the instance is 64-bit, otherwise false. I confirmed this works as expected by opening both PowerShell ISEs (32\/64) and the correct response was given. However, the command always returns false when running through AlwaysUp.<\/p>\n<p>Any help would be greatly appreciated.<\/p>\n<p align=\"right\">&mdash; Dustin<\/p>\n<\/div>\n<p>Hi Dustin, sorry to hear of the trouble! Thanks for doing the detective work to clearly illustrate the problem for our support team.<\/p>\n<p>The issue is caused by a bit of magic that Windows performs to maintain compatibility between 32-bit and 64-bit applications. Let&#8217;s explore some special folders.<\/p>\n<h2 class=\"blog-caption\">The System32, SysWOW64 and Sysnative folders<\/h2>\n<p>On your 64-bit system, 64-bit applications distributed with the operating system live in this folder:<\/p>\n<blockquote><p><b>C:\\Windows\\System32<\/b><\/p><\/blockquote>\n<p>Similarly, this folder holds the equivalent 32-bit versions of those Windows applications:<\/p>\n<blockquote><p><b>C:\\Windows\\SysWOW64<\/b><\/p><\/blockquote>\n<p>For example, we have the 64-bit version of powershell.exe in the System32 directory:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/powershell-in-system32.png\" class=\"zoomPopup\" title=\"powershell.exe in the System32 directory\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/powershell-in-system32.png\" title=\"powershell.exe in the System32 directory (click to enlarge)\" alt=\"powershell.exe in the System32 directory\" border=\"0\" \/><\/a><\/div>\n<p>While the 32-bit version resides in exactly the same place under the SysWOW64 folder:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/powershell-in-syswow64.png\" class=\"zoomPopup\" title=\"powershell.exe in the SysWOW64 folder\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/powershell-in-syswow64.png\" title=\"powershell.exe in the SysWOW64 folder (click to enlarge)\" alt=\"powershell.exe in the SysWOW64 folder\" border=\"0\" \/><\/a><\/div>\n<p>If you switch to the &#8220;Details&#8221; tab, you will see that both executables have exactly the same version number. In fact, they perform identically when run.<\/p>\n<p>Now here is where the magic comes in&#8230;<\/p>\n<p>When a 64-bit program launches an application under the System32 folder, Windows simply starts the application, as expected.<\/p>\n<p>However, when a 32-bit program tries to run an application that lives under the System32 folder, Windows:<\/p>\n<ol>\n<li>\n<p>Replaces <b>C:\\Windows\\System32<\/b> with <b>C:\\Windows\\SysWOW64<\/b> in the application&#8217;s path, and<\/p>\n<\/li>\n<li>\n<p>Runs the application from that SysWOW64 location.<\/p>\n<\/li>\n<\/ol>\n<p>For example, if a 32-bit program tries to start <b>C:\\Windows\\System32\\notepad.exe<\/b>, Windows will run <b>C:\\Windows\\SysWOW64\\notepad.exe<\/b> instead!<\/p>\n<p>Windows performs this &#8220;remapping&#8221; to provide stability for 32-bit applications, which often have trouble working with 64-bit applications. Microsoft thinks it is best to ensure that legacy 32-bit programs work with a legacy &#8220;32-bit view&#8221; of the operating system. Indeed, that makes sense when maintaining backward compatibility is top-of-mind.<\/p>\n<p>However, the price of that safety is that a 32-bit application can never directly invoke a 64-bit executable sitting in the System32 folder. If it tries, Windows simply rewrites the path to target the 32-bit executable instead (as mentioned in the example above).<\/p>\n<h3 class=\"blog-caption\">But then how does a 32-bit program start a 64-bit system utility?<\/h3>\n<p>If a 32-bit program really needs to invoke a 64-bit system executable, it can do so by citing the <b>Sysnative<\/b> virtual folder.<\/p>\n<p>For instance, if a 32-bit application wants to run the 64-bit version of notepad, it should invoke:<\/p>\n<blockquote><p><b>C:\\Windows\\Sysnative\\notepad.exe<\/b><\/p><\/blockquote>\n<p>In that way, the Sysnative folder makes it possible for 32-bit programs to call 64-bit operating system utilities.<\/p>\n<h2 class=\"blog-caption\">Why AlwaysUp launches the wrong version of PowerShell<\/h2>\n<p>AlwaysUp is a 32-bit application. As such, it is bound by the rules above.<\/p>\n<p>So when AlwaysUp launches the application you provided:<\/p>\n<blockquote><p><b>C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe<\/b><\/p><\/blockquote>\n<p>Windows actually starts:<\/p>\n<blockquote><p><b>C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe<\/b><\/p><\/blockquote>\n<p>which is the 32-bit version of PowerShell.<\/p>\n<h2 class=\"blog-caption\">How to make AlwaysUp launch the 64-bit PowerShell executable<\/h2>\n<p>To make AlwaysUp run the 64-bit version of PowerShell, specify this value in the &#8220;Application&#8221; field:<\/p>\n<blockquote><p><b>C:\\Windows\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe<\/b><\/p><\/blockquote>\n<p>Doing so will force Windows to invoke the version you want.<\/p>\n<p>Fortunately AlwaysUp is perfectly fine working with 64-bit applications, so you won&#8217;t run into any compatibility issues there. Please make the change and <a href=\"\/support\/\">let us know<\/a> if you don&#8217;t get the desired result!<\/p>\n<p>Even though we have a solution, there is one corollary we should mention&#8230;<\/p>\n<h2 class=\"blog-caption\">Why can&#8217;t I see the Sysnative folder?<\/h2>\n<p>Curiously, if you open the File Explorer and navigate to C:\\Windows\\Sysnative, the operation will fail. That&#8217;s because <b>the Sysnative folder is only accessible by 32-bit applications<\/b>. 64-bit programs (like the File Explorer you started) simply cannot see that directory.<\/p>\n<p>However, to prove for yourself that the folder exists for 32-bit applications, start a 32-bit command prompt by running this command:<\/p>\n<blockquote><p><b>C:\\Windows\\SysWoW64\\cmd.exe<\/b><\/p><\/blockquote>\n<p>Magically, the Sysnative folder will be visible from there:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/32-bit-cmd-accessing-sysnative-folder.png\" class=\"zoomPopup\" title=\"32-bit CMD accessing the Sysnative folder\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/32-bit-cmd-accessing-sysnative-folder.png\" title=\"32-bit CMD accessing the Sysnative folder (click to enlarge)\" alt=\"32-bit CMD accessing the Sysnative folder\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<!-- relpost-thumb-wrapper --><div class=\"relpost-thumb-wrapper\"><!-- filter-class --><div class=\"relpost-thumb-container\"><style>.relpost-block-single-image, .relpost-post-image { margin-bottom: 10px; }<\/style><h3>You may also like...<\/h3><div style=\"clear: both\"><\/div><div style=\"clear: both\"><\/div><!-- relpost-block-container --><div class=\"relpost-block-container relpost-block-column-layout\" style=\"--relposth-columns: 3;--relposth-columns_t: 2; --relposth-columns_m: 2\"><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/restart-service-no-task-scheduler\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"How to Make AlwaysUp Restart your Program Multiple Times per Day (without using the Task Scheduler)\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/stopwatch-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">How to Make AlwaysUp Restart your Program Multiple Times per Day (without using the Task Scheduler)<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/implementing-iso-27001-isms\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"How AlwaysUp Supports Your ISO 27001 ISMS\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/iso-27001-logo-150x150-1.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">How AlwaysUp Supports Your ISO 27001 ISMS<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/introducing-alwaysup-troubleshooter\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Introducing the AlwaysUp Troubleshooter: Helping you configure your application as a Windows Service\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/alwaysup-troubleshooter-150x150.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Introducing the AlwaysUp Troubleshooter: Helping you configure your application as a Windows Service<\/h2><\/div><\/div><\/a><\/div><!-- close relpost-block-container --><div style=\"clear: both\"><\/div><\/div><!-- close filter class --><\/div><!-- close relpost-thumb-wrapper -->","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp;We have been trying to get a particular PowerShell script running as a service for some time now. I have been pulling my hair out but I think I finally found the issue and I don&#8217;t understand why it&#8217;s happening. &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/running-powershell\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":10674,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[19,21,119,264,266,265,263],"class_list":["post-10560","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-alwaysup","tag-32-bit","tag-64-bit","tag-powershell","tag-sysnative-folder","tag-system32","tag-system32-folder","tag-syswow64-folder"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=10560"}],"version-history":[{"count":18,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10560\/revisions"}],"predecessor-version":[{"id":10930,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10560\/revisions\/10930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/10674"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=10560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=10560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=10560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}