PowerShell Output Field Separator ($OFS variable)

$OFS which is a special variable in Powershell. OFS stands for Output Field Separator. It exposes the .Net’s Systems.String object. as shown below

image

 

You would use this to separate objects in your array

For example:

If you run the below code to display objects in $array you’ll see the objects displayed one in each new line as that is the default object separator:

CODE:

$array = (1,3,5,7)
$array

 

OUTPUT:

1
3
5
7

 

 

 

By using $OFS variable, you’ll be able to specify your own separator character as shown below:

Note: as the OFS operates on strings, you need to convert the objects in array into String by using the type casting as in below example

CODE:

$array = (1,3,5,7)
$OFS =’^’;[string]$array

OUTPUT:

1^3^5^7

 

 

An alternate to specify your string objects separator at any point in time of your code is by referring the OFS variable as private.

CODE:

$private:ofs=”>”
$array = (1,3,5,7)
[string]$array

 

OUTPUT:

1>3>5>7

Leave a Reply

Your email address will not be published. Required fields are marked *