Scott Hanselman

Running PowerShell Scripts from the Command Line in a Hidden Window

February 28, '07 Comments [1] Posted in PowerShell | Tools
Sponsored By

Dave P. said the comments of a previous post "...but it's ugly b/c it actually opens a command window to do it. Batch files don't have this issue. Now if you can figure out a way to run powershell scripts in a hidden window, then you'd be onto something."

Running PowerShell Scripts from the command line is obscure and yucky to be clear, mostly because of escaping of quotes and such.

If you want to run a hidden powershell script, you can use HStart from the awesomeness that is NTWind Software. (Just go over there and download all of his utilities and save yourself some time).

Here's how to run a hidden powershell script:

hstart /nowindow "powershell.exe -command ""&" 'c:\DOCUME~1\Scott\desktop\foo.ps1'""

If you're going to start running hidden scripts like this, or any script, make sure that you've got PowerShell transcripts started with start-transcript and stop-transcript.

**********************
Windows PowerShell Transcript Start
Start time: 20070228113605
Username : MYDOMAIN\Scott
Machine : SUPERDUPERWONDERFUL-T60P (Microsoft Windows NT 5.1.2600 Service Pack 2)
**********************
Transcript started, output file is C:\Documents and Settings\Scott\My Docume
nts\WindowsPowerShell\Transcripts\20070228.113605.3196.txt

GAC Version Location
--- ------- --------
False v1.1.4322 C:\Documents and Settings\Scott\My Documents\Window...
Setting environment for using Microsoft Visual Studio 2005 x86 tools.

True v2.0.50727 C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0....

Hey! I'm in powershell (this is my script running)
**********************
Windows PowerShell Transcript End
End time: 20070228113605
**********************

You can make your scripts run faster by turning off the profile with the -noprofile switch, but be aware you'll have no profile and might not have some helper functions you've previously setup.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Thursday, March 01, 2007 3:29:36 PM UTC
Oh hell yeah. Once again, Scott, you are the all-seeing eye of useful utilities. Rock on.
Dave P.
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.