SOLVED

Correct my powershell please. Set-MsolUserLicence

%3CLINGO-SUB%20id%3D%22lingo-sub-1528102%22%20slang%3D%22en-US%22%3ECorrect%20my%20powershell%20please.%20MsolUserLicence%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1528102%22%20slang%3D%22en-US%22%3E%3CP%3EHey%20Guys%20relatively%20new%20to%20Powershell%20but%20creating%20a%20termination%20script.%20all%20working%20well%20and%20good%20apart%20from%20this%20section%20to%20remove%20365%20licences.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20what%20i%20want%20to%20be%20able%20to%20do%20but%20doesn't%20work%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24upn%3DGet-ADUser%20-Identity%20365.test1%20%7C%20Select%20Userprincipalname%3CBR%20%2F%3E(get-MsolUser%20-ObjectID%20%24upn).licenses.AccountSkuId%20%7C%3CBR%20%2F%3Eforeach%7B%3CBR%20%2F%3ESet-MsolUserLicense%20-UserPrincipalName%20%24upn%20-RemoveLicenses%20%24_%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20Does%20work%20but%20doesnt%20work%20in%20this%20script%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24upn%20%3D%20'365.test1%40domain.com'%3CBR%20%2F%3E(get-MsolUser%20-UserPrincipalName%20%24upn).licenses.AccountSkuId%20%7C%3CBR%20%2F%3Eforeach%7B%3CBR%20%2F%3ESet-MsolUserLicense%20-UserPrincipalName%20%24upn%20-RemoveLicenses%20%24_%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESeems%20to%20be%20something%20to%20do%20with%20my%20first%20line%20but%20i%20cannot%20figure%20it%20out.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1528102%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1528456%22%20slang%3D%22en-US%22%3ERe%3A%20Correct%20my%20powershell%20please.%20Set-MsolUserLicence%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1528456%22%20slang%3D%22en-US%22%3E%3CP%3EAny%20particular%20error%3F%20It%20looks%20OK%20to%20me.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1528475%22%20slang%3D%22en-US%22%3ERe%3A%20Correct%20my%20powershell%20please.%20Set-MsolUserLicence%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1528475%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Etried%20several%20variations.%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20i%20type%20in%20the%20UPN%20manually%20like%20above%20it%20works%20fine%20but%20when%20trying%20to%20get%20it%20by%20the%20Get-AdUser%20command%20it%20doesn't%20like%20it.%20Recognises%20its%20there%20though.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eget-MsolUser%20%3A%20User%20Not%20Found.%20%20User%3A%20%40%7BUserPrincipalName%3D365.test1%40domain.com.au%7D.%0AAt%20line%3A2%20char%3A2%0A%2B%20(get-MsolUser%20-UserPrincipalName%20%24upn).licenses.AccountSkuId%20%7C%0A%2B%20%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20OperationStopped%3A%20(%3A)%20%5BGet-MsolUser%5D%2C%20MicrosoftOnlineException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20Microsoft.Online.Administration.Automation.UserNotFoundException%2CMicrosoft.Online.Administration.Automation.GetUser%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1529353%22%20slang%3D%22en-US%22%3ERe%3A%20Correct%20my%20powershell%20please.%20Set-MsolUserLicence%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1529353%22%20slang%3D%22en-US%22%3E%3CP%3EOh%20sorry%2C%20I%20thought%20it%20was%20the%20other%20way%20around%2C%20my%20bad.%20So%20basically%2C%20you%20need%20to%20get%20just%20the%20UPN%20value%2C%20not%20the%20object.%20Either%20use%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%24upn.UserPrincipalName%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eor%20set%20the%20value%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%24upn%20%3D%20Get-AdUser%20blabla%20%7C%20select%20-ExpandProperty%20UserPrincipalName%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1534661%22%20slang%3D%22en-US%22%3ERe%3A%20Correct%20my%20powershell%20please.%20Set-MsolUserLicence%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1534661%22%20slang%3D%22en-US%22%3E%3CP%3EThankyou%20so%20much%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20was%20exactly%20what%20i%20needed%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

Hey Guys relatively new to Powershell but creating a termination script. all working well and good apart from this section to remove 365 licences.

 

This is what i want to be able to do but doesn't work:

 

$upn=Get-ADUser -Identity 365.test1 | Select Userprincipalname
(get-MsolUser -ObjectID $upn).licenses.AccountSkuId |
foreach{
Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $_
}

 

This Does work but doesnt work in this script:

 

$upn = '365.test1@domain.com'
(get-MsolUser -UserPrincipalName $upn).licenses.AccountSkuId |
foreach{
Set-MsolUserLicense -UserPrincipalName $upn -RemoveLicenses $_
}

 

Seems to be something to do with my first line but i cannot figure it out.

 

Thankyou in Advance

4 Replies
Highlighted

Any particular error? It looks OK to me.

Highlighted

@Vasil Michev 

tried several variations. 

When i type in the UPN manually like above it works fine but when trying to get it by the Get-AdUser command it doesn't like it. Recognises its there though.

 

get-MsolUser : User Not Found.  User: @{UserPrincipalName=365.test1@domain.com.au}.
At line:2 char:2
+ (get-MsolUser -UserPrincipalName $upn).licenses.AccountSkuId |
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Get-MsolUser], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Administration.Automation.GetUser

 

 

Highlighted
Best Response confirmed by Lucky1998 (Occasional Contributor)
Solution

Oh sorry, I thought it was the other way around, my bad. So basically, you need to get just the UPN value, not the object. Either use

 

$upn.UserPrincipalName

 

or set the value like this:

 

$upn = Get-AdUser blabla | select -ExpandProperty UserPrincipalName

Highlighted

Thankyou so much

 

That was exactly what i needed

 

@Vasil Michev