3

I have string below

1;#Chandler Bing 17;#Sheldon Cooper 18;#walter white

I'm trying to get user name from above string, in power shell I want to remove numbers and ;# please suggest some workaround

3

Just replacing the unwanted characters is quite easy. You can use regex just leaving letters in your string. But to make your output useful, and export content to a csv-file - you may need to trim your output further.

Here's my suggestion:

$text = "1;#Chandler Bing 17;#Sheldon Cooper 18;#walter white"

Write-Host $text -ForegroundColor Yellow

# Replacing non-alphabetic characters with space
$pattern = '[^a-zA-Z]'
$stripped = $text -replace $pattern, ' ' 
Write-Host $stripped -ForegroundColor Gray

# Replace three spaces with comma (,) if you want to output the string to a csv-file
$stripped = $stripped -replace "   ", ','
Write-Host $stripped -ForegroundColor Cyan

# Replace two spaces with one
$stripped = $stripped -replace "  ", ' '
Write-Host $stripped -ForegroundColor Red

# Remove the initial comma (,)
$stripped = $stripped.trimstart(",")
Write-Host $stripped -ForegroundColor Green

This outputs the following:

enter image description here

0

If you just want the names you can use following expression

("1;#Chandler Bing 17;#Sheldon Cooper 18;#walter white").Replace(/[^A-Za-z]+/g, '');

And here is the link for string operation in powershell

0

You can also split a string that looks like id;#text;#id;#text like this:

$separator = ";#"
$option = [System.StringSplitOptions]::RemoveEmptyEntries
$allLinks = yourstring.split($separator, $option)
foreach($entry in $allLinks) {
   write-host $entry
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.