A Blog on Wndows Server and Microsoft Exchange by Thomas Pätzold

Posts Tagged ‘Commandline

UNIX2DOS using Powershell

leave a comment »

Today I was asked to convert a file from the UNIX fileformat to the DOS fileformat.
As you probably know the format is different in the last character of each line. The DOS format ends with a carriage return (Cr) line feed (Lf) character whereas the UNIX format uses the line feed (Lf) character.
So this is a very simple task using Powershell

get-content <filename> |% {$_.replace("`n", "`r`n")} | out-file -filepath <new filename>

Remember the “%” means foreach-object. So this oneliner runs through the whole file and replaces the Lf with the CrLf character.

If you need a solution to use in a pipeline like the UNIX2DOS Unix command write a function that accepts the input from pipeline. The function looks like

function unix2dos{
param([Parameter(Mandatory=$true, ValueFromPipeline=$True)][String]$line)
begin {}
process {
$result = [string]$line.replace("`n","`r`n")
end {}

Call this function like:

get-content <filename> | unix2dos | out-file -filepath <new filename>

If you put this function in your Powershell profile, you are able to use is anytime anywhere.


Written by Thomas Pätzold

January 21, 2013 at 9:59 pm

The Exchange 2010 Powershell SnapIns

leave a comment »

Applies to Exchange 2010 SP1

If you start the Exchange Management Shell, a Powershell instance is started and the Powershell remoting feature is used in order to establish a connection to the Exchange Server System. There is another option to load the Exchange 2010 specific commands in Powershell. In this case you have to start the Powershell and load the necessary Exchange 2010 management snapins.

The following three Exchange 2010 Snapins are available


In order to load the snapins you are able to use the following command

add-pssnapin -Name Microsoft.Exchange.Management.PowerShell.E2010

If you are writing a Powershell script, it might be useful to check if the plugin is loaded. In order to do this you are able to use the following code snipset.

if (! (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:SilentlyContinue) )
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:Stop

What is the difference between the Exchange Management Shell and adding the Exchange snapin to the Powershell session?

  1. The Exchange commands using the Exchange Management Shell are implemented as functions. If you load the “Microsoft.Exchange.Management.PowerShell.E2010” snapin instead of using the Exchange Management Shell the commands are implemented as cmdlets.
  2. Using the Exchange Management Shell you are able to take a more detailed look at how the functions better called the wrapper functions are implemented by using the command.
    get-command get-mailbox -detailed | select-object scriptblock

  3. There is one more important diffrence. If you want to use the systemvariable $? it is better to use the loaded snapin instead of the Exchange Management Shell because these commands don´t work properly in the Exchange Managemnt Shell. A second problem is the use of the common parameters because they are not implemented for all functions in Exchange Management Shell.

If you want to start an Exchange Shell Session because you want to execute your command on a special Exchange Server then this is possible but keep the limitations of the Exchange Management Shell in mind.

Written by Thomas Pätzold

September 14, 2011 at 9:10 pm

%d bloggers like this: