Share via


Powershell command to read csv and remove license.

Question

Wednesday, February 1, 2017 3:39 PM

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 am trying this command but encounter an error about cannot bind argument:

Import-Csv "$env:c:\lic cleanup\departed users.csv" | ForEach-Object {$license = Get-MsolUser -UserPrincipalName $_.email Set-MsolUserLicense -UserPrincipalName $_.email -RemoveLicenses "$($license.license)"}

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

Thanks.

All replies (2)

Wednesday, February 1, 2017 5:21 PM

Hi.

Remove licenses from user accounts with Office 365 PowerShell

How to use Office 365 PowerShell to manage Microsoft Planner licenses

or orther example

Manage users in Office 365 using PowerShell

Provisioning and Licensing Office 365 Accounts with PowerShell

Office 365 License Reporting and Management Tool -Assign Remove Licenses in Bulk

MCITP, MCSE. Regards, Oleg


Thursday, February 2, 2017 4:39 PM

Hi,

Thanks for contacting our Exchange forum.

Create a notepad document that should include the following command and save it as .ps1 then run the file and check the results:

Import-Csv "$env:c:\lic cleanup\departed users.csv" | ForEach {Set-MsoluserLicense –UserPrincipalName $_.userprincipalname -RemoveLicenses "license name"}

For more professional assistance about scripts, I would like to recommended to post to Script center:

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?category=scripting

Hope it helps and thanks for your kindly understanding

Regards,

Jason Chao

Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact [email protected].