1 May 2013

Creating Unix Text Files with PowerShell

Posted by Juergen

Last week I was asked to help a colleague with a PowerShell script. The requirement was to write the primary SMTP address of all internal Exchange recipients to a file. The interesting part was that the file has to be written with the UNIX file format. A UNIX mail program will use the file to import it in its person based routing table.

I used the VIM editor to check the file format. Initially I tried the following two methods.

Get-Recipient -Filter { RecipientType -ne "MailContact" -and RecipientType -ne "MailUser"} | % { $_.PrimarySmtpAddress } | Out-File -FilePath D:\temp\File1.txt -Encoding UTF8

Get-Recipient -Filter { RecipientType -ne "MailContact" -and RecipientType -ne "MailUser"} | % { $_.PrimarySmtpAddress } | Set-Content -Path D:\temp\File2.txt -Encoding UTF8

Finally I used:

Get-Recipient -Filter { RecipientType -ne "MailContact" -and RecipientType -ne "MailUser"} | % -Begin { $str ="" } { $str += $_.PrimarySmtpAddress.ToString() + "`n" } -End {[System.IO.File]::WriteAllText("D:\temp\File3.txt",$str,[System.Text.Encoding]::UTF8)}

Comments are closed.

  • Browse

    or
  • Calendar

    April 2024
    M T W T F S S
    1234567
    891011121314
    15161718192021
    22232425262728
    2930  
  • Tag Cloud

  • Categories