Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag for United States of America

asked on

Powershell command to read csv and remove license.

I have a list of UPN of users in a xlsx file who is no longer with the company, and I'd like to remove their Office 365 licenses.  

Does someone advise a power shell command to read it and remove?

I can save the xlsx file to .csv if necessary.

Thanks.
Avatar of Jason Crawford
Jason Crawford
Flag of United States of America image

Your input file should have only one column entitled 'Email' (no quotes) and contain the primary email address of the user you're removing.  Save the file on your desktop as users.csv then try this:

Import-Module msonline
Connect-MsolService
Import-Csv "$env:USERPROFILE\Desktop\users.csv" | ForEach-Object {
    $license = Get-MsolUser -UserPrincipalName $_.email
    Set-MsolUserLicense -UserPrincipalName $_.email -RemoveLicenses "$($license.license)"
}

Open in new window


I'm assuming you have already installed the requirements for the msonline module.  If not follow this Technet article:

https://technet.microsoft.com/en-us/library/dn975125.aspx
Avatar of nav2567

ASKER

Thanks.

I am getting the attached error when executing the for loop.  Please see attached.
removelic.png
ASKER CERTIFIED SOLUTION
Avatar of Jason Crawford
Jason Crawford
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nav2567

ASKER

I have tried that.  Same result.  

This command is in the ps1 file.  

Import-Csv "$env:c:\dusers.csv" | ForEach-Object {$license = Get-MsolUser -UserPrincipalName $_.email Set-MsolUserLicense -UserPrincipalName $_.email -RemoveLicenses "$($license.license)"}
Did you try what I wrote or what you responded with?  The script in your reply has several errors so try running the script I provided exactly as I provided it