Powershell–Step on over to the DOSsy side of life

powershell

I can just imagine now.  Every English teacher I ever had.  Their eyes are rolling up within their heads like a Shark.

“DOSsy? DOSsy?  You made up a word called DOSsy?”

Sounds like somebody’s name.   Of course it isn’t but today I’m going to show you how to have Powershell pull down information from Dos as an Object that Powershell can work with.

Let’s try something simple.   There are Environment variables within Dos.  PATH, USERNAME and others.

If you need to access ANY of those variables in Powershell just tack on $ENV: before the name

$ENV:PATH

$ENV:USERNAME

There you go.   All that information is easily accessible.

But wait.  Did you say there is Console output from an older application you’d like to work with in Windows Powershell?  Not a headache either.  Generally, if it echoes to the Console you can grab it in Powershell.

So let’s try something that could be useful.   A list of all the variables in Dos.   Normally in DOS I would just key in

SET

and afterwards a nice list would appear on my screen.  Wonderful.  But unless I want to “Copy Paste” that information (not very useful) It’s just sitting there in DOS land.

But JUST LIKE Powershell, you can pass parameters to CMD.EXE.   Just key in CMD.EXE /? in any shell (Dos or Powershell) to see the parameters.  The “C” (Command) parameter is the one we’re interested in.

So If I want to pass the “SET” command to Dos as a scripted function I would execute

CMD.EXE /C SET

Now would you like to work with that information?  You can.  Pipe that through any Powershell Cmdlet, say “FOREACH” to step through the information

CMD.EXE /C SET | FOREACH { WRITE-HOST This Value is $- }

Neat eh?  But we can even get fancy and Pull the Values and Variables apart into a cleaner list.   There is a "split” method in Dotnet (You can see it if you PIPE it through GET-MEMBER)

CMD.EXE /C SET | GET-MEMBER

With Split you can pull of a really neat trick Dos can’t easily do.  You can “SPLIT” lines of text into new lines by giving it a Delimeter.   A Character that says “The line stops here".  So if I want to split my Variables and Values in Dos, I’ll use the Equals sign as my Delimeter since THAT is the part that separates them

CMD.EXE /C SET | FOREACH { $-.split(“=”) }

Now for the cool part.   When I split this ONE object, each piece of that split piece can now be referenced INDIVIDUALLY.   In my case, I’d like to look at just the FIRST piece (which will always be the variable) in each line.  I can do that easily by specifying it as an array.  Since the first member of an array ALWAYS starts at Zero (0)

CMD.EXE /C SET | FOREACH { $-.split(“=”)[0] }

Cool, you now have a list of the current variables accessible.   Silly?  I think not.   It’s not difficult to leverage this SAME method to grab Content from other Commands or output in CMD.EXE   This is just a simple example.

Remember, the Power of Shell is in YOU

Sean
The Energized Tech

Leave a Reply