PowerShell for Windows Admins

Jan 20 2014   12:54PM GMT

Win32_Examples–start application in hidden window



Posted by: Richard Siddaway
Tags:
CIM
PowerShell 3
PowerShell v4

This one is interesting as I’d tried doing this a while back and failed.  Starting a process with Win32_Process is straightforward but controlling the process – such as starting in a hidden window wasn’t working. This is how you do it:

function start-hiddenproc {

[CmdletBinding()]

param ( [string]$processname = ‘notepad.exe’ )

$startclass = Get-CimClass -ClassName Win32_ProcessStartup

$startinfo = New-CimInstance -CimClass $startclass -Property @{ShowWindow = 0} -ClientOnly

$class = Get-CimClass -ClassName Win32_Process

Invoke-CimMethod -CimClass $class -MethodName Create -Arguments @{Commandline = $processname; ProcessStartupInformation = [CimInstance]$startinfo}

}

The function takes a path to the process executable as a parameter – would be a good place for a validation script parameter to test the path to the executable.

Get the Win32_ProcessStartup class and use it with New-CimInstance to create the start up information. The New-CimInstance parameter –ClientOnly can be aliased to –Local.  I always prefer to use the master parameter name rather than aliases – makes it easier for people to look things up in the documentation.

Get the Win32_Process class and use it with Invoke-CimMethod to invoke the Create method with the arguments passed as shown

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: