Solved

powershell special characters

Posted on 2014-09-05
11
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 35

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
Is Your AD Toolbox Looking More Like a Toybox?

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.

 
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 70

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
 

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 70

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
In this video we show how to create a Shared Mailbox 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 Recipients >> Sha…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

688 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