{"id":20,"date":"2006-12-20T06:48:50","date_gmt":"2006-12-20T14:48:50","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/20\/schedule-hidden-powershell-tasks\/"},"modified":"2018-04-11T10:04:09","modified_gmt":"2018-04-11T17:04:09","slug":"schedule-hidden-powershell-tasks","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/20\/schedule-hidden-powershell-tasks\/","title":{"rendered":"Schedule Hidden PowerShell Tasks"},"content":{"rendered":"<p>I&#8217;ve had some questions lately about scheduling a PowerShell script. This is something we&#8217;ve covered a little bit. You recall that you can run a PowerShell command from CMD:<\/p>\n<pre>powershell -nologo -command \"&amp; {c:\\scripts\\myps.ps1}\"<\/pre>\n<p>You can include commands like this into a batch file or use the expression as a scheduled task. If you want to completely hide the PowerShell output you can use an expression like this:<\/p>\n<pre>powershell -nologo -noninteractive -command \"&amp; {c:\\scripts\\myps.ps1}\"<\/pre>\n<p>This works great when running PowerShell directly from CMD or in a batch file. However if you are trying to hide the window when running as a scheduled task, this doesn&#8217;t have any effect. In order to hide the window, you need to wrap your PowerShell command in a VBScript and use the Run method of the Shell object. This method lets you specify a Window style, including hiding it.\u00a0 Here&#8217;s a wrapper script you can use.<\/p>\n<pre>Dim objShell,objFSO,objFile\r\n\r\nSet objShell=CreateObject(\"WScript.Shell\")\r\nSet objFSO=CreateObject(\"Scripting.FileSystemObject\")\r\n\r\n'enter the path for your PowerShell Script\r\n strPath=\"e:\\documents and settings\\jhicks\\my documents\\scripts\\posh\\Get-DiskSize.ps1\"\r\n\r\n'verify file exists\r\n If objFSO.FileExists(strPath) Then\r\n   'return short path name\r\n   set objFile=objFSO.GetFile(strPath)\r\n   strCMD=\"powershell -nologo -command \" &amp; Chr(34) &amp; \"&amp;{\" &amp;_\r\n    objFile.ShortPath &amp; \"}\" &amp; Chr(34)\r\n   'Uncomment next line for debugging\r\n   'WScript.Echo strCMD\r\n\r\n  'use 0 to hide window\r\n   objShell.Run strCMD,0\r\n\r\nElse\r\n\r\n  'Display error message\r\n   WScript.Echo \"Failed to find \" &amp; strPath\r\n   WScript.Quit\r\n\r\nEnd If<\/pre>\n<p>The only thing you need to do is put in the path and filename to your PowerShell script. Because of the way PowerShell and CMD struggle with long filenames, the wrapper script gets the short path of your script. This removes the need to struggle with quotes, which trust me, which you don&#8217;t want to deal with. Save the VBScript and create your scheduled task. Be sure to specify WSCRIPT to run your script and not CSCRIPT, otherwise you&#8217;ll still get a command window popping up.<\/p>\n<p>I know it seems like a lot of work, but I&#8217;ve done most of it for you.\u00a0 All you need to do is put your PowerShell script path in the wrapper. IMPORTANT: This wrapper assumes you want to run a PowerShell script. It will not work with a PowerShell cmdlet or expression. I&#8217;ll see what I can do about that another day.<\/p>\n<p>(I&#8217;ve attached a copy of the script as a text file. It&#8217;s less than 1K so it shows up as 0)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had some questions lately about scheduling a PowerShell script. This is something we&#8217;ve covered a little bit. In this entry I discuss how to wrap your PowerShell script in a VBScript and schedule it so that it runs completely hidden.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,24,25],"tags":[],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-general","category-vbscript","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":10,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":14781,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions\/14781"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}