append string to variable in for each loop

#Contents of foobar1.csv =
#Entry in Azure AD =

Question: How can i add the phrase “” to the UPN (spreadsheet contents)


Import-Csv D:\Powershell_temp\foobar1.csv | ForEach-Object {
$ObjectId = (Get-AzureADUser -ObjectId ‘$_.Userprincipalname “”’).ObjectId

Here is error message:

Get-AzureADUser : Error occurred while executing GetUser
Code: Request_BadRequest
Message: The request URI is not valid. Since the segment ‘Microsoft.DirectoryServices.User’ refers to a collection, this must be the last segment in the request URI. All intermediate
segments must refer to a single resource.
HttpStatusCode: BadRequest
HttpStatusDescription: Bad Request
HttpResponseStatus: Completed
At line:2 char:18

Thank you for your input


Variables can’t be put in single quotes.
You can d below.
‘$_.Userprincipalname “”’ will not be the one you expect.

Import-Csv D:\Powershell_temp\foobar1.csv | ForEach-Object {
$ID = $_.Userprincipalname + ''
$ObjectId = (Get-AzureADUser -ObjectId $ID).ObjectId

Hello Kvprasoon;

Thank you for your response it worked!!


Great, If you were not aware of this before, I would request you to read the documentation about quoting rules.

About Quoting Rules