Solved

powershell special characters

Posted on 2014-09-05
11
342 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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 AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
Read this checklist to learn more about the 15 things you should never include in an email signature.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
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…

919 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

17 Experts available now in Live!

Get 1:1 Help Now