Powershell - Cannot convert value to type "System.Int32"

Eaven HUANG 2,166 Reputation points
2021-07-27T06:45:52.713+00:00

Dear experts,

I'm working on below script trying to replace the value of mailNickName with the prefix part of UPN value. It works great if we had EMPTY mailNickName field (where it showed <not set>). However, if there was value already existing in mailNickName field, in this example the value is test2 and the UPN prefix is test2.mcs

Cannot convert value "test2" to type "System.Int32". Error: "Input string was not in a correct format."
At D:\OneDrive - GTIIT\IT Dept\PowerShell\Scripts\Case_Study\Replace mailNickName with UPN prefix\test.ps1:9 char:26
+                      if ($UPN_Prefix -ne -$_.mailNickname){
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

Below is the source code:

$OUs = "OU=Eaven-Testing,OU=Users,OU=XXX,DC=edu,DC=cn"
 foreach ($OU in $OUs) {
     Get-ADUser -Filter * -SearchBase $OU -Properties samAccountName, userPrincipalName, mailNickname, Enabled | 
         Where-Object { $_.Enabled -eq $True} |
             ForEach-Object{
                 $parts = $_.userPrincipalName -split '@'
                 $UPN_Prefix = $parts[0]
                 if ($_.mailNickname){
                     if ($UPN_Prefix -ne -$_.mailNickname){
                         $was = $_.mailNickName
                         Set-ADUser -Identity $_.samAccountName -Replace @{mailNickname="$UPN_Prefix"}
                     }
                     else{
                         Write-host "$UPN_Prefix already exists, no change will be made on it."
                     }

                 }
                 else{
                     $was = "EMPTY"
                     Set-ADUser -Identity $_.samAccountName -Add @{mailNickname="$UPN_Prefix"}
                 }
                 [PSCustomObject]@{
                     Identity = $_.samAccountName
                     UPN = $_.userPrincipalName
                     NicknameWas = $was
                     NickNameNow = $parts[0]
                 }
             } | Export-Csv -NoType 'C:\tmp\userPrincipalName_vs_mailNickname.csv'
 }
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,549 questions
{count} votes

Accepted answer
  1. Ian Xue 37,541 Reputation points Microsoft Vendor
    2021-07-27T07:20:14.607+00:00

    Hi,

    You should remove the extra - prepended to $_.mailNickname in LIne 9.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.