wolfmaster74
wolfmaster74

Reputation: 17

Powershell - Group names- How to append to group name unless append already exists

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} 

Upvotes: 1

Views: 96

Answers (1)

scottwtang
scottwtang

Reputation: 2040

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")
    }
}

Upvotes: 2

Related Questions