Home > Software design >  Powershell - Group names- How to append to group name unless append already exists
Powershell - Group names- How to append to group name unless append already exists

Time:07-06

I am trying to append to a group display name unless the append already exists currently my code is as below and it works on the first pass but if you re run it it appends the text each time again.

I would like a way to check if the "-Yammer" already exists in display name and skip if it does. Any clever ways of achieving this ?

Many Thanks

Get-UnifiedGroup -ResultSize Unlimited |?{$_.GroupSku -eq "Yammer"} |  Export-Csv "C:\Temp\yammerGroup.csv" -NoTypeInformation

Import-CSV "C:\Temp\yammerGroup.csv" | ForEach-Object {
Set-UnifiedGroup -Identity $_.id -DisplayName ($_.DisplayName   "-Yammer")}
{
Set-UnifiedGroup -Identity $_.id -HiddenFromAddressListsEnabled $False} 

CodePudding user response:

You can use an if statement with the EndsWith method to check if the end of the group name matches your specified string.

Note EndsWith is case-sensitive, so I've also added ToLower to convert the group name to lower-case first.

Import-CSV "C:\Temp\yammerGroup.csv" | ForEach-Object {
    if (! ($_.DisplayName.ToLower().EndsWith("-yammer") ) )
    {
        Set-UnifiedGroup -Identity $_.id -DisplayName ($_.DisplayName   "-Yammer")
    }
}
  • Related