Powershell O365 - Unlicesing users

I have a .csv file where there is a username and an email address.  The user is already in AD with a different email address, and is licensed for O365.  I need the script to remove the old email address, input the new one from the csv file and remove O365 licensing. When I run the script, I receive an error:

Get-ADUser : Cannot find an object with identity: 'name@name.com' under: 'some OU'.
At line:58 char:2
+ Get-ADUser $userName | Move-ADObject -TargetPath $OU
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (name@name.com:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Set-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'EmailAddress'. Specified method is not supported.
At line:52 char:33
+ Set-ADUser $user -EmailAddress $primaryemail -Company "some company"
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Set-MsolUserLicense : User Not Found. User: .
At line:46 char:2
+ Set-MsolUserLicense -UserPrincipalName $user@company.com -RemoveLic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.SetUserLicense

get-msoluser : User Not Found. User: @{user=username; primaryemail=someuser1@anothercompany.com}@company.com.
At line:43 char:9
+ $a = get-msoluser -UserPrincipalName $user@company.com | select user ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Get-MsolUser], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.GetUser

Below is the script I'm using.  When we receive the .csv file it only has the username, so the username needs to be "converted" to the userprincipalname in order for the "Get-msol" and the "Set-msol" commands to work as they do not work with the username.

#Imports the AD module
Import-Module ActiveDirectory

#Imports the MSonline module
Import-Module MSOnline

#Credentials to license or unlicense users in O365
$Username = "username"
$Password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential $Username, $Password

Connect-MsolService -Credential $cred


#Gets the ocicu.csv file
$path = Import-Csv "importfile" -header("user","primaryemail")

#Function to remove license
Function removelicenses {
$a = get-msoluser -UserPrincipalName $user@email.com | select userprincipalname, licenses
$licensed = $a.licenses.accountskuid
$u = $a.userprincipalname
Set-MsolUserLicense -UserPrincipalName $user@email.com -RemoveLicenses "License 1", "License 2" -ErrorAction SilentlyContinue

#Function to input non-company email address and chg company name
Function inputemail
Set-ADUser $user -EmailAddress $primaryemail -Company "company name"

#Function to move users
Function moveusers {

Get-ADUser $userName | Move-ADObject -TargetPath $OU

Foreach ($user in $path)

#calls functions
$error | out-file -filepath "logfile"

Any assistance would be greatly appreciated!

  • Hey! Your foreach loop is incorrect.

    You have to reference the properties like this: $user.user or $user.primaryemail (since you are using the variable $user in the foreach)

    Something like this:

    $a = get-msoluser -UserPrincipalName $user.user@email.com | select userprincipalname, licenses

    Set-MsolUserLicense -UserPrincipalName $user.user@email.com -RemoveLicenses "License 1", "License 2" -ErrorAction

    Set-ADUser $user.user -EmailAddress $user.primaryemail -Company "company name"