Author Posts

April 11, 2016 at 8:53 am

Hi,

I've read last month a way to do what I want, but I lost the article and can't find it =/

I have a variable with this information gathered from a text file using Get-Content:

Carl Marx (carl.marx@contoso.com); William Shakespeare (william.shakespeare@contoso.com); Paul Newman (paul.newman@contoso.com); Bruce Willis (bruce.willis@contoso.com)

I want the variable to have only this:

Carl Marx; William Shakespeare; Paul Newman; Bruce Willis

How can I remove the "(mail@contoso.com)" information easily?

April 11, 2016 at 9:01 am

$var = $var -replace " ;",";"

Would be one way.

April 11, 2016 at 9:10 am

Don Jones,

Sorry, you must read the post as I've been trying to insert the (mail@contoso.com) text...
I was trying using "<" to insert...

April 11, 2016 at 9:22 am

This example should work. Copy and paste the part you need (remove mail or add mail).

$string = "Carl Marx (carl.marx@contoso.com); William Shakespeare (william.shakespeare@contoso.com); Paul Newman (paul.newman@contoso.com); Bruce Willis (bruce.willis@contoso.com)"

# Remove mail for each
$removemail = $string -split ';' -replace "\(.*\)",";"
$removemail.Trim()
# Result:  Carl Marx ;

# Add mail for each
$addmail = $string -split ';' -replace "@",".mail@"
# Result: Carl Marx (carl.marx.mail@contoso.com)

April 11, 2016 at 10:02 am

random commandline,

Thanks! Worked perfectly =]

April 12, 2016 at 4:14 am

Just something a little more simple. You can use just -replace with RegEx for this.

$input = "Carl Marx (carl.marx@contoso.com); William Shakespeare (william.shakespeare@contoso.com); Paul Newman (paul.newman@contoso.com); Bruce Willis (bruce.willis@contoso.com)"
$input -replace " \(.*?\)"

Results:

Carl Marx; William Shakespeare; Paul Newman; Bruce Willis