String manipulation using PowerShell

Posted by SysAdmin Tools on

WE HAVE FREE AND PREMIUM TOOLS

WE ALSO HAVE A BLACK FRIDAY SPECIAL RUNNING


Adding to a string

$strValue = "Test"
$strValue += "ing"

$strValue
Testing

 

Removing info from a string using Replace()

$stvValue = "Testing"
$strValue = $strValue.Replace("ing","")

$strValue
Test

 


Remove part of a string using Remove()

$strValue = "Test String"

Remove from index 4 to end of string
$strValue.Remove(4)
Test

Remove from index 0 to 5
$strValue.Remove(0,5)
String

 

Remove part of a string using Substring()

$strString = "Test String"

Remove from 0 to index 4
$strValue.Substring(0,4)
Test

Remove from index 5 to end of string
$strValue.Substring(5)
String

 

Split a string into multiple sections and select only the part you need using its index.

Very useful for email addresses

$StrValue = "username@domain.com"
$strValue.Split('@')
username
domain.com

$strValue.Split('@')[0]
username

CLICK ON THE BANNER TO CHECK OUT OUR FREE AND PREMIUM TOOLS HERE

 

To Lowercase() or Uppercase()


"TEST".ToLower()
test

"test".ToUpper()
TEST

 

Remove Whitespace (either spaces at beginning or end)

Remove whitespace at beginning and end of string
$strValue = "Test String "

$strValue.Length
12

$strValue.Trim().Length
11

TrimStart()
Removes Whitespace at the beginning of the string
TrimEnd()
Removes Whitespace at the end of the string


Remove specific part of a string that has a dynamic index

$strValue = "This is a test string"
$stringIndex = $strValue.IndexOf("string")
$stringIndex
15

$strValue.Remove($stringIndex)
This is a test

$strValue.Substring(0,$stringIndex)
This is a test
 

Change a string to a date format

$stringDate = "2021 04 01"

$Date = [datetime]::Parse($stringDate)
$Date
Thursday, 01 April 2021 00:00:00

 

Change a date to a string

$Date.ToString("yyyy-MM-dd")
2021-04-01

More date formats

 


 

CLICK ON THE BANNER TO CHECK OUT OUR FREE AND PREMIUM TOOLS HERE


Share this post



← Older Post Newer Post →


Leave a comment

Please note, comments must be approved before they are published.