February 17, 2010
Posted by on
I thought I would start a new feature. I have a document I keep in dropbox that keeps my little Powershell notes. It has quick and dirty tips. Kind of a Powershell Cookbook of my own workings. I thought I would share them since they are so useful to me (with the added benefit that I can search my site for them when I need them).
Powerbit #1 comes from a forum posting as most of my ideas seem to come from that revolve around Powershell. The question posed is how can you run Powershell from a command line without having a powershell window pop up. This is especially key for scheduled tasks to ensure that little windows don’t litter your console. Fortunately my little Powerbits cheat sheet had an answer.
Let’s first look at Powershell.exe -? to see what it has to offer:
There are two parameters that look like they will solve our problem.
Hides the copyright banner at startup.
Sets the window style to Normal, Minimized, Maximized or Hidden.
Unfortunately when you try the following from start->run you’ll see that it does pop open a window for a brief moment:
powershell.exe -nologo -windowstyle hidden -command "dir c:\users\ttanasovski\scripts\*.*|out-file c:\users\ttanasovski\scripts\test.txt"
There are two solutions to this problem. One is to load up visual studio and compile an executable to launch the Powershell command or script. The other is to wrap the command in a vbs script so that you can use the Run method of WSH:
command = "powershell.exe -nologo -command ""dir c:\users\ttanasovski\scripts\*.* |out-file c:\users\ttanasovski\scripts\test.txt"""
Set shell = CreateObject("WScript.Shell")
Problem solved. Thank you Powerbits sheet!