Run command line command in powersehll

Pete
Pete used Ask the Experts™
on
A simple question, what's the best was to run a command line command in powershell?

In my .ps1 script after a load of powershell commands I want to run a line:

Rundll printuientry.....etc

Do I use invoke command or invoke expression or something else?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
I would first look for a native Powershell command.

For example, there are better ways to automate printer without use rundll32 printui.dll,printUIEntry

After that, I prefer to use System.Diagnostics.Process mainly because I develop in .NET
$process = New-Object System.Diagnostics.Process

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
There is no need for that. It's called PowerShell for a reason - it's not only a scripting engine like VBScript, it's a shell for interactive use as well, and a such offers convenient access to command line tools. You can usually start external programs in a ps1 script the same way as from a PowerShell console, from a cmd.exe console, or from a batch file: just call them.
Example:
net use X: \\SomeServer\SomeShare

Open in new window

By convention (especially in scripts, not necessarily in an interactive command line), you should add the .exe extension, and use the "&" call operator to make it apparent that this is an external program, not a cmdlet, function, or alias.
& net.exe use X: \\SomeServer\SomeShare

Open in new window

One caveat: if the program (or its path) contains a space, the & becomes mandatory, because you'll need quotes around the path, turning it into a simple string as far as PS is concerned. PS would normally just send it down to stdout, instead of executing it. The & tells PS to execute it.
& "C:\Program Files\Windows NT\Accessories\wordpad.exe"

Open in new window

So in your script, you could just use
& Rundll.exe printuientry.....etc

Open in new window


That said, and as Shaun mentioned already, depending on your PS/OS version, there might be native cmdlets available to do something that required command line tools earlier.
A good start is the Get-Command cmdlet, which supports filters.
Example:
Get-Command -Noun *Print*

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial