How construct a variable that works with export-csv?

%3CLINGO-SUB%20id%3D%22lingo-sub-3043949%22%20slang%3D%22de-DE%22%3EHow%20construct%20a%20variable%20that%20works%20with%20export-csv%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3043949%22%20slang%3D%22de-DE%22%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EHello%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EI%20have%20a%20small%20csv%20file%20that%20I%20import%20in%20Powershell%20into%20a%20variable.%20Here%20it%20is%20(as%20I%20can't%20seem%20to%20upload%20a%20file)%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%22RGG%22%2C%22BAS%22%2C%22IMT%22%0A%22keepass%22%2C%22webex%22%2C%22firefox%22%0A%22acrobat%22%2C%22ciscoany%22%2C%22teams%22%0A%22%22%2C%22keepass%22%2C%22Visio%22%0A%22%22%2C%22Acrobat%22%2C%22Project%22%0A%22%22%2C%22gekkotArt%22%2C%22%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EAfter%20importing%20it%20I%20can%20examine%20%24myCsvVar%20in%20many%20ways.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EFor%20example%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24myCsvVar%3D%20Import-Csv%20-Path%20C%3A%5Ctemp%5CcsvFile.csv%20-Delimiter%20%22%3B%22%0A%0A%24myCsvVar%0A%24myCsvVar.RGG%20%0A%24myCsvVar.RGG%5B1%5D%0A%24myCsvVar%5B1%5D%0A%24myCsvVar%5B1%2C2%5D%0A%24myCsvVar.gettype()%0A%24myCsvVar.RGG.gettype()%0A%24myCsvVar%5B1%5D.gettype()%0A%24myCsvVar%7Cget-member%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EHowever%20hard%20I%20try%2C%20I%20am%20not%20able%20to%20create%20such%20a%20variable%20from%20within%20Powershell.%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EWould%20anybody%20here%20know%20how%20to%20do%20that%3F%3C%2FSPAN%3E%3CSPAN%20class%3D%22%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EThe%20reason%20is%20that%20I%20have%20to%20construct%20a%20variable%20as%20an%20object%20that%20I%20then%20can%20use%20with%20export-csv.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EThank%20You.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22%22%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3043949%22%20slang%3D%22de-DE%22%3E%3CLINGO-LABEL%3Econstruct%20variable%20for%20export-csv%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

Hello,

I have a small csv file that I import in Powershell into a variable. Here it is (as I can't seem to upload a file):

 

 

 

"RGG","BAS","IMT"
"keepass","webex","firefox"
"acrobat","ciscoany","teams"
"","keepass","Visio"
"","Acrobat","Project"
"","gekkotArt",""

 

 

After importing it I can examine $myCsvVar in many ways.

For example:

 

 

$myCsvVar= Import-Csv -Path C:\temp\csvFile.csv -Delimiter ","

$myCsvVar
$myCsvVar.RGG 
$myCsvVar.RGG[1]
$myCsvVar[1]
$myCsvVar[1,2]
$myCsvVar.gettype()
$myCsvVar.RGG.gettype()
$myCsvVar[1].gettype()
$myCsvVar|get-member

 

 

However hard I try, I am not able to create such a variable from within Powershell.

Would anybody here know how to do that? 

The reason is that I have to construct a variable as an object that I then can use with export-csv.

Thank you.

 

3 Replies
Usually you create an array with all the fields (colums) that you need and export it to a .csv using export-csv with the -NoTypeInformation parameter.

Example export to csv with a list of files in c:\temp

[array]$contents=Get-ChildItem c:\temp
$contents | export-csv -Path c:\temp\x.csv -Delimiter ';' -NoTypeInformation

Hello,
thank you for looking into this.
When I pull contents from my small csv file into a variable, I have no problem to export it again.
However, as I need to get things from here and there, strings mostly, I figured I would construct a variable just like the one I get after importing my csv file.
But that does not work. As long as I pull objects into a variable it is okay. But constructing a variable and putting strings from different places into it seems impossible.
So far I helped myself by writing into a file, knowing that the data in csv must be homogenous troughout.
It would have been elegant to construct the variable first as an object, give it the noteproperties (members) I need and only do one export-csv in the end.
Good New Year 2022 to you - in any case.

@AndiPsh180 

Hi,

One solution would be to create an array of pscustomobjects like so:

 

$myArr=@()
for($i=0; $i -lt 10; $i++){
    $myArr = $myArr + [PSCustomObject]@{AAA = ""; BBB = ""; CCC = "" }
    }

 

Afterwards I can set any cell of the matrix:

 

$myArr[2].aaa="note"

 

The only problem is that I have more then three keys, all through to ZZZ.

As I don't want to type them all, I wonder whether I can get this done programmatically?

I tried to build a string variable and put it within the curly brackets of the hashtable. But the compiler does not allow for that as it requires a hard coded equal sign.

Anybody have an idea?

Thank you