Running an Executable in Powershell

Posted: 20th January 2015 by Ben in For the Forums

It’s a beautiful week here in January, we’ve had temperatures nearing near the 60’s and it’s been beautiful outside. According to the weather man, it will be colder (like winter is supposed to be) but hey: I’ll take what I can get.

I want to address several different ways to execute Windows Executables today. I typically try not to do this in Powershell, honestly. I like to create ways to do what I’m doing in another fashion- but sometimes instead of re-inventing the wheel it’s impossible, and I do like automation and saving time more.

So here are a few methods to go about executing .exe files within Powershell, and my experience in using these methods.  NOTE:  These are not ALL methods.  I may bring  a few others up later, but these are the ones I found myself researching the most.

1) Directly through Powershell-  This is if it already has an environment variable or things that are within the windows\system32.  These are typically a handful of programs, things that are included within the OS suite when loaded.  For instance, ping.exe is located within the environment variable, so you can simply call it within a Powershell console by typing:  ping myhostwhatever.  If you are trying to simplify your work and think that moving the executable you are trying to run with Powershell into the system32 directory, think twice about your security posture and patching and read on!

2)  Invoke-Command- This method is a commandlet within Powershell.  This commandlet uses WinRM (WinRM), which is usually never enabled on the machines I work on.  I know there are tons of articles, methods and even approved methods to enable it, but my security bound heart screams no to me.  The only way I would do it is if the infrastructure permanently clicked the “on” switch, so I write my code according to knowing that I can’t use it.   However, I digress from the subject.  The Invoke-Command can be used to execute commands on the remote (or local) system.  So, you can do things like copy scripts to remote machines, then use Invoke-Command to execute them,  run commands on the remote computer such as ‘ipconfig /all’ for a simple example and it would return back to the machine.  It is super convenient if you have it enabled in your environment.

3)  Invoke-ExpressionInvoke-Expression is very similar to the Invoke-Command except for it’s not.  Invoke-Expression will execute whatever you pass to it.  So you can run it on the remote machine to do things like zip files, unzip files, troubleshoot things like group policy, domain policies, etc.  This also takes WinRM, so keep that in mind if you find something that works awesome on your local machine but then go to test it in your domain.

4)  Invoke-Item-  Invoke Item to me is like a magic trick gone bad.  It’s like you are sitting at a child’s birthday party and the magician is trying to produce a rabbit from his hat, but 22 rabbit’s come out.  Invoke-Item will open a file with the associated program that Windows assigned.  So, if you use it to open up a text file Invoke-item something.txt that file will open visually with notepad in front of your eyes.  The true bad magician trick though, if you have a whole folder of 300 text documents and you navigate to the directory and type:  Invoke-Item *.txt  Then all 300 text documents open at once.  I’ve only ever used this on a local machine and have no idea if it can be used remotely, except in partnering with Invoke-Command (as you wouldn’t use it with Invoke-Expression because you use Invoke-Expression for things that don’t have builtin commandlets.)  EDIT:   I did some research and this in fact can be used remotely, I personally wouldn’t recommend it unless you are in Computer 101 with your friends.

5)  The Mighty ‘&’ Symbol-  I like the ‘&’ ability within Powershell.   Within Powershell the & is called the call operator and it allows you to execute a command, script or function.  This for all intensive purposes will do exactly what you want.  It’s quick and dirty and as long as you structure it good, you will have no problem.   If you want to simply run an executable with Powershell, you can execute:  & “d:\scriptlocation\awesomeprogram.exe” and it will run it.  Very quick and dirty, but with no real troubleshooting or logic built in.

6)  [Diagnostics.Process] Start()-  Now THIS is a nice way to perform a task in  clean method.  One of my favorite reasons for using Powershell is creating .NET usage on the fly just by calling it.  No need to build up variables, as in C# because Powershell does all the work for you.  You can read about the .NET class here and I suggest looking into it.  You can build a complete process for executing an executable, complete with error control and everything.


$sdp = new-object System.Diagnostics.Process
$sdp.StartInfo.Filename = "netstat.exe"
$sdp.StartInfo.Arguments = " -an"
$sdp.StartInfo.UseShellExecute = $false
$sdp.WaitForExit()  ## LOVE this WaitForExit

I love the “Wait for exit” portion of this, because you can execute it and it will wait until the code as completed before it moves on.  Perfect for logic and/or loops!



I needed this today and it took me forever to figure it out, hopefully it saves some folks sometime

string rgx = @"^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)/(3[0-2]|[1-2]?[0-9])$";

Powershell for Cisco?!

Posted: 14th November 2014 by Ben in For the Technical
Tags: , ,

Yeah, I know it’s been a while. I’ve been busy!

Let’s move on from that though. I want to start sharing things again.

First of those things is the “PowerPack” from Cisco!!
Located here

I did mention this is for the Unified Computing System? You don’t know what this is? Ok now you do.

So the amount of cmdlets here are incredible, I think there is somewhere in the 1800 neighborhood.

How do I know that?
(Get-Command | ? {$_.ModuleName -eq “CiscoUcsPS”}).Count

So many possibilities here… I’m normally a “Get” verb guy, when starting out with a new set of cmdlets, so let’s do that:

(Get-Command -Verb “Get” | ? {$_.ModuleName -eq “CiscoUcsPS”}).Count

That is a grand total of 992 items. That is crazy.

Over the next few weeks I’m going to deep dive into a lot of these, so stay tuned.

Recently used my flash drive for a project and when I went to format it again so I can reload an operating system, I noticed it was not utilizing all of the space.

When I went to format it, I saw that it was only showing max: 2.81 gigs (which was the size of the live CD boot I had on there.) I did some digging and found that the system was in fact recognizing the space, however I couldn’t utilize the unused portion.

What I did to fix it was pull open good ol’ disk part.

If you aren’t familiar with diskpart then tread lightly. It’s a very good tool but also very powerful.

Here is how to complete it:

DISKPART> select disk 3
Disk 3 is now selected
Diskpart succeeded in cleaning the disk.
DISKPART>create partition primary
Diskpart succeeded in creating the specified partition

All is good now and I can utilize all the unused space once again.

Hope that helps someone.

I don’t normally post pictures…

Posted: 7th January 2014 by Ben in For the Technical

But this is hilarious:

ITT Tech is two ipconfig and google maps hardcore!

It’s true, it can’t.
So- you have to download the Vmware OVF Tool

Convert that badboy ovf to a vmx to use in Workstation!

That’s not what you wanted? Oh you are going from vmx to ovf? I’m sorry- here are both ways to do that:
(Assuming you have a environment variable to the ovftool- if not just go to the source C:\Program Files\VMware\…

conversion from vmx to ovf:
ovftool f:/somepath/os.vmx c:\temp\os.ovf

conversion from ovf to vmx:
ovftool f:/somepath/os.ovf c:\temp\os.vmx

Now you are on your way to Virtual Machine goodness!

You sometimes come up with weird desires to do things in programming and scripting and today I needed to increment my filenames not by number, but by alphabet increments. To top it off, not only alphabet entries, but two string alphabet entries! (e.g. aa, ab, ac)

So to start off we have this wonderful tidbit:

$a = 97..122 | foreach {[char]$_}

Ok so that takes care of one part of it, we have a-z and that is a perfect array.
Some of you might wonder why the hell that worked. It’s because the “..” is a range character for powershell. That means that it will print the distance of the specified. So if we didn’t convert that to a character, it would have counted 97-122 in an array (which we stored conveniently).

How did I know that the 97-122 converted to a character would be what I needed?


And you guessed it- I did the same for ‘z’.

So now we need to allow it to count aa, ab, ac and so on and so forth. It took me a minute to logic through this, but if we just duplicate work a bit- we can let powershell do the rest.

$a = 97..122 | foreach {[char]$_}

foreach ($damnNumber in $a)
$item = 97..122 | foreach {[char]$_}
foreach ($damnItem in $item)
$damnNumber + "" + $damnItem}

Now we can apply that logic to whatever we are working on for the minute.
Hope that helps

Got an email this morning asking for help with a script! I enjoy this! Kind of why I made the site, except I have a terrible memory too and it actually helps me as well.

So here is the email I got this morning, left out the personal information.

Message: Hi

Could you help me with script?
I need list of backup files from folder but my script doesnt work properly.

Get-ChildItem “\\machinename\G:Backup$” | Select-Object Name, CreationTime, @{Name=”Kbytes”;Expression={$_.Length / 1Kb}}

Thanks for help.

So in looking at this- the Get-ChildItem is the correct commandlet for the job here. We know that Get-ChildItem works like the old school, “dir” command- so this works.

Just looking at the syntax, you may want to check the share name too, usually with a share, I’ve seen a “$” after the G. So in otherwords:

Just check it though, not sure of your setup. The easiest way would just be to navigate to it through the client computer.

next- Your select statement is awesome. Your label looks great and works like a charm- grats on that.

Adam- another thing to keep in mind is that you must have rights with the account running the script . So if you wrote the script as your user account that doesn’t have rights to the share, right click your powershell ISE or command prompt first and run as a different user and run with admin credentials that have access to the share.

Let me know if that works, doesn’t work!

Got this error recently while trying to run firefox in my Ubuntu server:

Firefox is already running, but is not responding. To open a new window, you must first close the existing Firefox process, or restart your system.

Being a novice at most linux processes, I didn’t want to just get the fix I wanted to go through the troubleshooting steps.

Obviously there was a problem with a task running, so I took to find a command that would kill processes, simple enough I found a command called kill.

I wasn’t sure which profile the task was running on so I was able to use the command:
killall firefox
And it worked! Gotta be honest, the killall command is pretty sweet sounding anyways..

I’ve had about enough of this…


Set DisableCMD to 0.