powershell what if statement displayname contains certain characters

I have my powershell exchange code but trying to figure out the best way to add an if statement for display names containing special characters like &($. I would like to write those to a separate csv file if possible and then the rest to another csv file.


@ (

 "microsoft.com/users/accounting"
 "microsoft.com/users/finance"
 "microsoft.com/users/computers"
      
) | % {

$stat1 = Get-mailbox -resultsize:unlimited –organizationalunit $ou_dir | foreach-object {

  $stat2 = get-mailboxstatistics $_

   
 

 

    new-object PsObject -Property @{

     PrimarySmtpAddress = $_.PrimarySmtpAddress

     "number of items"          = $stat2.ItemCount


     UseDatabaseQuotaDefaults = $_.UseDatabaseQuotaDefaults

     OU   = $_.identity

     IssueWarningQuota  = $_.IssueWarningQuota

     ProhibitSendQuota  = $_.ProhibitSendQuota

     "totalitemsize" = $stat2.TotalItemSize.Value.ToMB()

     Alias              = $_.Alias

     office              = $_.office

     Database           = $_.database

     RecipientTypeDetails       = $_.RecipientTypeDetails

     SingleItemRecoveryEnabled   = $_.SingleItemRecoveryEnabled

  }

} | select Alias, RecipientTypeDetails, Office, SingleItemRecoveryEnabled, Database, "totalitemsize", PrimarySmtpAddress, ItemCount,OU, UseDatabaseQuotaDefaults, identity, IssueWarningQuota,prohibitSendQuota   | export-csv c:\report.csv –nti
techdriveAsked:
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.

SubsunCommented:
Try..
$report = @(
"microsoft.com/users/accounting"
"microsoft.com/users/finance"
"microsoft.com/users/computers"
) | % {
Get-mailbox -resultsize:unlimited –organizationalunit $ou_dir | foreach-object {
  $stat2 = get-mailboxstatistics $_
    new-object PsObject -Property @{
     DisplayName = $_.DisplayName
		 Alias = $_.Alias
		 PrimarySmtpAddress = $_.PrimarySmtpAddress
     "number of items" = $stat2.ItemCount
     UseDatabaseQuotaDefaults = $_.UseDatabaseQuotaDefaults
     OU = $_.identity
     IssueWarningQuota = $_.IssueWarningQuota
     ProhibitSendQuota = $_.ProhibitSendQuota
     totalitemsize = $stat2.TotalItemSize.Value.ToMB()
     office = $_.office
     Database = $_.database
     RecipientTypeDetails = $_.RecipientTypeDetails
     SingleItemRecoveryEnabled = $_.SingleItemRecoveryEnabled
  }
 }
} | select DisplayName,Alias,Office,SingleItemRecoveryEnabled,Database,totalitemsize,"number of items",PrimarySmtpAddress,ItemCount,OU,UseDatabaseQuotaDefaults,IssueWarningQuota,prohibitSendQuota
$report | ?{$_.DisplayName -match "[&($]"} | export-csv c:\SpecialDisplayreport.csv –nti
$report | ?{$_.DisplayName -notmatch "[&($]"} | export-csv c:\NormalDisplayreport.csv –nti

Open in new window

If you want to match any special characters, you can try replacing "[&($]" with "[\W]"
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
The other way is to collect the data into two different vars (one for each case) in the loop, and then export those, but it doesn't make a difference in efficiency (only sketched, to emphasize the difference):
@ ( ... ) | % { ...
} | select ... |  % {
  if ($_.DisplayName -match '[&($]') { $Special += $_ } else { $Normal += $_ }
}
$Special | export-csv -NTI c:\SpecialDisplayreport.csv
$Normal | export-csv -NTI c:\NormalDisplayreport.csv

Open in new window

This way is better if you would also need to process the "special" cases further.
0
techdriveAuthor Commented:
one more last thing subsun. I would like for this to work with prompting for an OU please.

Add-Type -AssemblyName Microsoft.VisualBasic
$export_dir = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter the complete path of the output file", "Output file",  "C:\temp")
Add-Type -AssemblyName Microsoft.VisualBasic
$onedir = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter the OU path in the object field in AD", "Object", "EX: microsoft.com/users/accounting")
Get-mailbox -resultsize:unlimited –organizationalunit $onedir| foreach-object {
  $stat2 = get-mailboxstatistics $_
    new-object PsObject -Property @{
     DisplayName = $_.DisplayName
             Alias = $_.Alias
             PrimarySmtpAddress = $_.PrimarySmtpAddress
     "number of items" = $stat2.ItemCount
     UseDatabaseQuotaDefaults = $_.UseDatabaseQuotaDefaults
     OU = $_.identity
     IssueWarningQuota = $_.IssueWarningQuota
     ProhibitSendQuota = $_.ProhibitSendQuota
     totalitemsize = $stat2.TotalItemSize.Value.ToMB()
     office = $_.office
     Database = $_.database
     RecipientTypeDetails = $_.RecipientTypeDetails
     SingleItemRecoveryEnabled = $_.SingleItemRecoveryEnabled
  }
 
} | select DisplayName,Alias,Office,SingleItemRecoveryEnabled,Database,totalitemsize,"number of items",PrimarySmtpAddress,ItemCount,OU,UseDatabaseQuotaDefaults,IssueWarningQuota,prohibitSendQuota
$report | ?{$_.DisplayName -match "[^(|]"} | export-csv c:\SpecialDisplayreport.csv –nti
$report | ?{$_.DisplayName -notmatch "[^(|]"} | export-csv c:\NormalDisplayreport.csv –nti
0
SubsunCommented:
Try..
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$ou_dir = [Microsoft.VisualBasic.Interaction]::InputBox("Please enter the OU path in the object field in AD")

$report = Get-mailbox -resultsize:unlimited –organizationalunit $ou_dir | foreach-object {
  $stat2 = get-mailboxstatistics $_
    new-object PsObject -Property @{
     DisplayName = $_.DisplayName
		 Alias = $_.Alias
		 PrimarySmtpAddress = $_.PrimarySmtpAddress
     "number of items" = $stat2.ItemCount
     UseDatabaseQuotaDefaults = $_.UseDatabaseQuotaDefaults
     OU = $_.identity
     IssueWarningQuota = $_.IssueWarningQuota
     ProhibitSendQuota = $_.ProhibitSendQuota
     totalitemsize = $stat2.TotalItemSize.Value.ToMB()
     office = $_.office
     Database = $_.database
     RecipientTypeDetails = $_.RecipientTypeDetails
     SingleItemRecoveryEnabled = $_.SingleItemRecoveryEnabled
  }
}| select DisplayName,Alias,Office,SingleItemRecoveryEnabled,Database,totalitemsize,"number of items",PrimarySmtpAddress,ItemCount,OU,UseDatabaseQuotaDefaults,IssueWarningQuota,prohibitSendQuota

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

Open in new window

0

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
0
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
Powershell

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.