Follow sqlserver-dba.com

Subscribe to RSS feed  Follow @jackvamvas - Twitter

*Use the Comments section for questions

SQLServer-DBA.com Links

Dba_db2_button

dba-ninja.com

SQL Server DBA Jobs

How to use Powershell string functions

25 June,2012 by Tom Collins

When developing Powershell scripts for managing SQL Server Instances , eventually you will need to manipulate some strings.

In this case , I had to get the server name of a SQL Server instance reference – the part before the ‘\’ .

Use the Powershell Substring  and IndexOf  functions – for deriving a substring based on the position of a delimiter.

I’ve included examples of LTRIM,RTRIM and substring with a set position.

 

$instance = "MYSERVER\MYINSANCE"

#LTrim
$instance.TrimStart()
#RTrim
$instance.TrimEnd()
#Substring
$instance.Substring(1,4)
#IndexOf (equivalent of CHARINDEX) 
$instance.IndexOf('\')
#substring based on IndexOf
$instance.Substring(0,$instance.IndexOf('\'))

Author: Tom Collins (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to use Powershell string functions


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer