powershell special characters

I need some assistance with trying to pull displaynames that contain special characters. For example lets say I have

$report | ?{$_.DisplayName -match "[&($]"} | export-csv c:\SpecialDisplayreport.csv –nti
$report | ?{$_.DisplayName -notmatch "[&($]"} | export-csv c:\NormalDisplayreport.csv –nti

I am trying to pull special characters &($ or if I need to replace them with others is this the samething. when I pull this portion the specialdisplayreport gets everything but the normaldisplayreport file  is empty.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dan CraciunIT ConsultantCommented:
Can you please post some sample data? For the report variable.
The code looks OK, so it would be easier to test on your data.

I am not clear about the request are you saying the filter is not working?
Does the display names in SpecialDisplayreport contain '&' or '(' or '$' ?
techdriveAuthor Commented:
Sorry the filter is not working. It is picking up everything rather than the names with special characters/.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

PS C:\> $report = "test","test$","test(","Test&"

PS C:\> $report | ?{$_ -match "[&($]"}


PS C:\> $report | ?{$_ -notmatch "[&($]"}


PS C:\>

Open in new window

I don't see any reason for the filter to fail. Are you using the exact filter written in the comment?
?{$_.DisplayName -match "[&($]"}
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The condition works in general. Proof:
"Test & Result", "Test $ Result", "Test NoResult" | ? { $_ -match "[&($]" }

Open in new window

So it has to be something on the input ($result).
techdriveAuthor Commented:
I did the same test you guys performed with a list of just the displaynames. I did notice that for some reason putting the ^ character before the | character yielded different results. For example  ? { $_ -match "[^|]" } works differently than  ? { $_ -match "|^" } If I place the latter names would display all but if I place the first example it gets everything.

Could it be because the ^ character represents anything at the beginning? I really just need this for the first character to be processed and was thinking to refine this. Is there a way to evaluate the displaynames or character set that begins with one of these special characters please?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
"|^" doesn't make sense. It is "nothing (= everything) or something at the beginning".
You can escape those characters with a backslash were needed. Putting them into square brackets switches to a different context, with most characters being used literally, but not the caret. A caret as first character in square brackets negates the match. "[^a-z]" is "not a small case letter". "[a-z^]" though is "a small letter or a caret".
You want to match some special characters, but only if they are the first character? Then just use something along '^[&($|]'. Also make sure the pattern is enclosed in single quotes, to prevent from the PS engine to evaluate potential expressions (the dollar sign and parens belong to vars and subexpressions).
[&($] Matches any single character in the specified set of characters.

Adding anchor ^ means start of the string, but when [^inside brackets], it means "not".

[^&($] Matches any single character not in the specified set of characters inside the bracket.

In your scenario, If you are trying to match all special characters then better you can try with  
?{$_.DisplayName -match "\W"}  it will match all special characters except '_' (underscore)

\W is equivalent to [^a-zA-Z_0-9].


?{$_.DisplayName -match "[^\s\w]" if you want to exclude space also.

I am still not clear, if you are looking to match specific set of special characters or all of them.. :-)  Probably you can give us some examples to better understanding..

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
techdriveAuthor Commented:
thanks guys I think I have got it now. I have learned a lot from you two. As soon as I am in front of a machine will try. I cannot tell you how much I am appreciative of your assistance.
techdriveAuthor Commented:
Subsun to directly answer your question I have found that we have accounts that start with a special character either @#^ or | after analyzing the data. So what you gave me ?{$_.DisplayName -match "[^\s\w]" will suffice. I will also try Qlemo's code ?{$_.DisplayName -match '^[&($|]' thanks again.
Did you get expected results with the suggested patterns? If you need only to match characters @#^ or |, then you can try..

?{$_.DisplayName -match "[@#^|]"}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.