30

I'm working on creating a script that will fire off performance counters and store them in a .csv file, rolling over when the file gets too big. Running the script from a powershell prompt or the ISE (I'm running v.2/win server 2008) works just fine, and the files are running correctly.

However, when I try to execute the command using the following cmd prompt line (even trying the /k switch on cmd), the command prompt closes immediately and the file is not getting run.

powershell.exe -windowstyle hidden {iis_test.ps1}

I've also tried the following, and although I see Powershell doesn't stop (I monitor it via task manager, looking for Powershell.exe to show up), I'm not seeing the file being created.

powershell.exe -noexit -windowstyle hidden {iis_test.ps1}

Nor am I seeing any errors being caught in any of the likely places (event viewer, the command prompt window itself).

Any ideas? Ultimately, my goal is to have a vb winforms thing call these script files, and naturally I thought figuring out the command prompt command would be helpful. Note that it is very important that this script be portable, and run on any Windows 2008 Server system (so I need to stay away from third party entities that would require further installation).

EDIT: I needed to use the -file argument to specify a file to run.

2 Answers 2

49

try this from a DOS/CMD shell:

powershell.exe -windowstyle hidden -file C:\iis_test.ps1
Sign up to request clarification or add additional context in comments.

5 Comments

this still displays a window, although briefly upon starting
hmm I use pwsh -windowstyle hidden C:\script.ps1 and it still works. Any idea?
tried this pwsh.exe -windowstyle hidden -file C:\script.ps1 it pops up the window at first then minimizes it automatically, but not like in background.
This method will fail if the extension is other than .ps1. If the script is a .bat, this is the command: Start-Process "script.bat" -WindowStyle Hidden
12
Powershell.exe -windowstyle hidden -file C:\iis_test.ps1

But when you run this command, a CMD window will appear.
However, you can use VBscript instead

Set objShell = CreateObject("WScript.Shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -file " & "YourScript.ps1", 0, False
Set objShell = Nothing

1 Comment

how to use thi VBscript? is is some kind of bat file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.