RaSor's Blog

December 23, 2011

Powershell with .NET

Filed under: Administration, NET, Powershell — rasor @ 10:01 am

This post is where I want to put how to do things from .NET in powershell.

There are some automatic conversions, for instance, that makes code different.

Using string.Split()

[char]$domsep = '\'
[string]$userid = "CONTOSO\XANDH"
#Split will in .NET accept a char[], but here it is OK to give the char directly without the array
$userid = $userid.Split($domsep)[1] #XANDH
#
#In most cases you can use the powershell way to do split as below, but that does not work, when the split char is '\'
#$userid = ($userid -split "\")[1] #XANDH
#In case of split char is '\' you need to escape it with an extra '\' as follows:
$userid = ($userid -split "\\")[1] #XANDH

Using String.IsNullOrEmpty()

if (-not [System.string]::IsNullOrEmpty($someString))
{
}

Add CRLF to strings

$crlf = "`n`r"
$Body = "Testing CRLF:" + $crlf
$Body += "Bla-bla"

Links:

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.