Name Export via Powershell not working as expected

I am trying to dump application data from Azure into their own file, however, when i reference the DisplayName attribute for the application to save as the file name I get a weird naming convention. The code below imports the csv with the DisplayName and Object ID. Attached is sample file as well

import-csv .\AzureApplications.csv | ForEach {Get-AzureADApplication -objectID $_.ObjectID |export-csv .\$_.DisplayName.csv}

Open in new window

The output file name is as follows:
@{DisplayName=Test Application; ObjectId=10f3cd35-bf7e-468d-bdbb-298fa456b3c5; AppId=440ba5b1-0372-4b62-9379-edf7096c8cb5}.DisplayName.csv
Any help would be appreciated
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
The problem is that you're using "$_.Var" inside a string and is not being evaluating, So you're getting the whole Object


Your code:
Import-csv .\AzureApplications.csv | ForEach {Get-AzureADApplication -objectID $_.ObjectID |export-csv .\$_.DisplayName.csv}

Open in new window

My code, you do need to evaluate "$" before sending it into the "string"
Import-csv .\AzureApplications.csv | ForEach {Get-AzureADApplication -objectID $_.ObjectID |export-csv .\$($_.DisplayName).csv}

Open in new window

And you do this like :  with $()  $_.DisplayName  ... so $($_.DisplayName)

Basically if you have an object X, defined as  $X=@{A="valueA";B="valueB"} (2 properties A and B).
if you're gonna get the value of the property A, if it's a string all alone:  The name of a file for example, would be
 $X.A  + ".csv"

Open in new window

(like in C# variable + " string")

if you want to do it "inline", then you use $( expression ), to evaluate 'expression':

Open in new window

In this case the line is evaluated 1st the value inside the brakets ... ($X.A). and the result is dumped into the string

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Jose is correct. But as here, it is not obvious that a parameter is a string, because PowerShell tries to do its best in "helping" you to use parameters and types:
If the filename expression starts with a bracket or dollar, it would first get evaluated as expression and return a string.
In other cases the "expression" is seen as a string, with inline string evaluation only - that is what you see.
In fact. it is much more complicated ;-). But in general you can assume that file name parameters are (implicit) strings. Get-Content, Import-*, Export-* are examples for that behaviour.
ntr2defAuthor Commented:
Thank you Jose for the re-education. It has been over a year since I have done programmatic administration and as you were explaining my immediate reaction was "Duh!"
Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
Yeah? haha, so much fun! :)
Read the @Qlemo comment and as well as @odba I respect them much, especially on the Powershell area, they're gods of it :)
And I'm glad to help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.