More fun with scheduled PowerShell

Earlier, I gave you a VBScript wrapper that you could use to run PowerShell scripts as scheduled tasks and be completely hidden from the user.  If you want to run a PowerShell cmdlet expression instead of a .ps1 script, you need to take a slightly different approach.

You can use this VBScript as a wrapper:

Dim objShell
Set objShell=CreateObject(“WScript.Shell”)

‘enter the PowerShell expression
‘you need to use short filenames and paths
strExpression=”get-process | where {$_.workingset -ge 50000000} | out-file e:\docume~1\jhicks\results.txt”

strCMD=”powershell -nologo  -command ” & Chr(34) &_
“&{” & strExpression &”}” & Chr(34)

‘Uncomment next line for debugging
‘WScript.Echo strCMD

‘use 0 to hide window
objShell.Run strCMD,0

The strExpression is one long line. The important thing to remember is that if you are referencing a file, as I am here, you must use a short file name.  Especially when it contains spaces. The better approach for my example would have been to pipe output-file to something like c:\logs\results.txt.  But I wanted to make a point about long file names. As with the other VBScript wrapper, when you create your scheduled task use WSCRIPT.