Run command line command in powersehll

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
LVL 1
PeteAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun VermaakTechnical Specialist/DeveloperCommented:
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

0
oBdACommented:
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

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.