Friday, October 25, 2013

Working with PowerShell Preference Variables: $OFS (Output field Separator).


When you open PowerShell console, type $ and start pressing ‘tab’, you will amaze that there are many pre-built variables exists there.

One of the variable is $OFS, this is a preference variable and full meaning of $OFS is ‘Output Fields Separator’.

What it does?

It specified the character to use as separator while returning the result from the array. By default when the results of an array come, it has a single space as a separator.

By default the value of  $OFS is null.

Let me show to you.

I am using PowerShell version 3.0. , and this $OFS variable is exists in PowerShell version 2 too.


When you type and run the  $OFS variable the result is blank.


For testing, let’s create a array of the script and checked the output.

[string]$myStringArray = 'Aman', 'Singh' , 'Dhally'


Now, Lets run the “$myStringArray ” variable.

You can see that, all items in our array result are separated by a single space.


Now let’s assign a semi-colon as a separator in $OFS variable.

 $OFS = ";"


Now create a array of string variable same as before.

Now , you can see, when you run the $myStringArray variable this time, all items of the array are separated with semi-colon rather then like single space before.


You can use comma as a separator,


or anything you like,  ;)


this is very handy, when you are planning to export data in to CSV files, you can have the option to choose separators of your choice  rather then using the single space.

Happy Learning.

Tested on Windows 7 [64 bit]


Aman Dhally

clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

No comments:

Post a Comment