Posts Tagged ‘Powershell’

Adding Line Numbers to Native Command Output

December 4, 2007

One of the great things about Powershell is that you can use all your standard command line tools, such as ipconfig and net.exe. The only issue is that these tools output text and Powershell works with objects.

The output of commands such as ipconfig is actually an array.

PS 24 >  (ipconfig).GetType().BaseType.FullName 
System.Array

Each item in that array is a string

PS 31 >  (ipconfig)[4].gettype().FullName 
System.String

So really you end up with an array of strings and if you know the indexes of that array, you can work with much more specific data. For example:

PS 58 >  (ipconfig)[7] 
   IPv4 Address. . . . . . . . . . . : 10.2.4.48

But how do you know that this is line 7, or the 7th item in the array. You can run the command and count lines and guess, or you can do something like this: