Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>$OFS - <a href="http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!6957.entry" rel="noreferrer">output field separator</a>. A handy way to specify how array elements are separated when rendered to a string:</p> <pre><code>PS&gt; $OFS = ', ' PS&gt; "$(1..5)" 1, 2, 3, 4, 5 PS&gt; $OFS = ';' PS&gt; "$(1..5)" 1;2;3;4;5 PS&gt; $OFS = $null # set back to default PS&gt; "$(1..5)" 1 2 3 4 5 </code></pre> <p><a href="http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!816.entry" rel="noreferrer">Always guaranteeing you get an array result</a>. Consider this code:</p> <pre><code>PS&gt; $files = dir *.iMayNotExist PS&gt; $files.length </code></pre> <p>$files in this case may be $null, a scalar value or an array of values. $files.length isn't going to give you the number of files found for $null or for a single file. In the single file case, you will get the file's size!! Whenever I'm not sure how much data I'll get back I always enclose the command in an array subexpression like so:</p> <pre><code>PS&gt; $files = @(dir *.iMayNotExist) PS&gt; $files.length # always returns number of files in array </code></pre> <p>Then $files will always be an array. It may be empty or have only a single element in it but it <em>will</em> be an array. This makes reasoning with the result much simpler.</p> <p>Array covariance support:</p> <pre><code>PS&gt; $arr = '127.0.0.1','192.168.1.100','192.168.1.101' PS&gt; $ips = [system.net.ipaddress[]]$arr PS&gt; $ips | ft IPAddressToString, AddressFamily -auto IPAddressToString AddressFamily ----------------- ------------- 127.0.0.1 InterNetwork 192.168.1.100 InterNetwork 192.168.1.101 InterNetwork </code></pre> <p><a href="http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!6159.entry" rel="noreferrer">Comparing arrays using Compare-Object</a>:</p> <pre><code>PS&gt; $preamble = [System.Text.Encoding]::UTF8.GetPreamble() PS&gt; $preamble | foreach {"0x{0:X2}" -f $_} 0xEF 0xBB 0xBF PS&gt; $fileHeader = Get-Content Utf8File.txt -Enc byte -Total 3 PS&gt; $fileheader | foreach {"0x{0:X2}" -f $_} 0xEF 0xBB 0xBF PS&gt; @(Compare-Object $preamble $fileHeader -sync 0).Length -eq 0 True </code></pre> <p>Fore more stuff like this, check out my free eBook - <a href="http://tinyurl.com/efposh" rel="noreferrer">Effective PowerShell</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload