Thursday, October 18, 2012

How To: Run Several Programs Sequentially in PowerShell [Field Notes]

Problem
I want to run several installation programs in order, and don't want them to step on each other.

I want to avoid errors such as "another setup program is already running" which result in the second install not completing.

Solution
For files without arguments, run:
$var1 = Start-Process -FilePath "[path]" -passthru
$var1.WaitForExit()

For files with arguments, run:

$var1 = Start-Process -FilePath "[path]" -ArgumentList "[Args]" -passthru
$var1.WaitForExit()


In these examples, [path] is the full path to the file (e.g. C:\SomeFolder\MyProgram.exe) you want to run. [Args] is whatever you'd normally put after the path to the exe in the command line.

NOTE: Single quotes around the file path will ensure the command will not break if it includes a space in the path.

References

How To: Change a Drive Letter With PowerShell [Field Notes]

Problem

I need to change the letter of a mounted drive via PowerShell.

Solution

Start Powershell as an admin and run the following two lines, where 'x' is the current drive letter and y is the drive letter you'd like it to be:

$drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'x:'"
Set-WmiInstance -input $drive -Arguments @{DriveLetter="Y:";}

References