Solved

powershell special characters

Posted on 2014-09-05
11
338 Views
Last Modified: 2014-09-10
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.
0
Comment
Question by:techdrive
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 40305781
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.

Thanks.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40305787
I am not clear about the request are you saying the filter is not working?
Does the display names in SpecialDisplayreport contain '&' or '(' or '$' ?
0
 

Author Comment

by:techdrive
ID: 40305924
Sorry the filter is not working. It is picking up everything rather than the names with special characters/.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40305965
PS C:\> $report = "test","test$","test(","Test&"

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

test$
test(
Test&

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

test

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 "[&($]"}
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40305975
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).
0
Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

 

Author Comment

by:techdrive
ID: 40308472
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?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40308488
"|^" 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).
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40308736
[&($] 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].

Or

?{$_.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..
0
 

Author Comment

by:techdrive
ID: 40308842
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.
0
 

Author Comment

by:techdrive
ID: 40308844
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.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40313136
Did you get expected results with the suggested patterns? If you need only to match characters @#^ or |, then you can try..

?{$_.DisplayName -match "[@#^|]"}
0

Featured Post

Are your corporate email signatures appalling?

Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now