1

how do I read arrays from a file into variables with powershell? My ListOfFileNames.txt has arrays which are like this one:

#Begin ArrayOneList:
Filename4.txt
FilenameD.png
myone.exe

#Begin ArrayTwo:
myFile2.txt
Filename1.gif

#Begin ArrayNextOne:
speficifFile.doc
ExcelFile.xls
File3.png
AnotherFile.png
logFile.log

Now I want to read the Arrays into Variables and work with them in Powershell:

$ArrayOneList = Get-Content C:\Scripts\ListOfFileNames.txt
foreach($File in $ArrayOneList)
{
    $Weekday = $File.LastWriteTime.DayOfWeek
    $Destination = "D:\Test\$Weekday"

    Copy-Item $File.FullName -Destination $Destination -Recurse    
}


$ArrayTwo = Get-Content C:\Scripts\ListOfFileNames.txt
foreach($File in $ArrayTwo)
{
    $Weekday = $File.LastWriteTime.DayOfWeek
    $Destination = "E:\Dir\Dest"

    Copy-Item $File.FullName -Destination $Destination -Recurse    
}


$ArrayNextOne= Get-Content C:\Scripts\ListOfFileNames.txt
foreach($File in $ArrayNextOne)
{
    $Weekday = $File.LastWriteTime.DayOfWeek
    $Destination = "E:\Dir\SpecificDirectory"

    Copy-Item $File.FullName -Destination $Destination -Recurse    
}

How can I read all the Arrays from One File in to Variables?

3 Answers 3

0

If you can use JSON as format in your input file you can simply "deserialize" the array. Example:

PS C:\temp> [string[]]$array = @("1","2","3")

PS C:\temp> ConvertTo-Json $array
[
   "1",
   "2",
   "3"
]

PS C:\temp> ConvertTo-Json $array | Out-File array.json

PS C:\temp> Get-Content .\array.json |  ConvertFrom-Json
1
2
3

PS C:\temp> [string[]]$array2 = Get-Content .\array.json |  ConvertFrom-Json 

PS C:\temp> $array2.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                          
-------- -------- ----                                     --------                                                                                                                          
True     True     String[]                                 System.Array   

PS C:\temp> $array2
1
2
3

To work with multiple arrays you can create a hashtable that contains these arrays. Example:

PS C:\temp> $arr1 = @("1","2")

PS C:\temp> $arr2 = @("3","4")

PS C:\temp> $arrays = @{"arr1"=$arr1; "arr2"=$arr2}

PS C:\temp> $arrays

Name                           Value                                                                                                                                                         
----                           -----                                                                                                                                                         
arr1                           {1, 2}                                                                                                                                                        
arr2                           {3, 4}                                                                                                                                                        


PS C:\temp> $arrays | ConvertTo-Json
{
     "arr1":  [
                "1",
                "2"
              ],
     "arr2":  [
                "3",
                "4"
              ]
 }

 PS C:\temp> $arrays | ConvertTo-Json | Out-File hash.json

 PS C:\temp> $recreatedHash = get-content .\hash.json | ConvertFrom-Json 

 PS C:\temp> $recreatedHash

 arr1   arr2  
 ----   ----  
 {1, 2} {3, 4}

Hope that helps

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, this seems to be understandable for experts but not for me. Where do I call my ListOfFileNames.txt? Where do I declare the name of my Arrays "ArrayOneList,ArrayTwo,ArrayNextOne..." ?
0

You can do like this:

param(
    [parameter(Mandatory=$true)]
    [string[]]$myarr
)

Refer THIS Example also

In your case, you can do like this:

param(
    [parameter(Mandatory=$False)]
    [string[]]$myarr=@("ArrayOneList","ArrayTwo","ArrayNextOne")
)

4 Comments

Do I've to call it like this?: Powershell.exe -File "MyScriptPath-ps1" "ArrayOneList,ArrayTwo,ArrayNextOne" How can I use the parameters withouth the calling of the arrays from outside and only from inside of my script?
@Mailo: Yes, you have to call like that. THe link which I have given in that you can see some examples also for that
There is no example how ich can use it withouth the calling of parameters from outside. Ich just want to call the poweeshellfile.ps1 withouth parameters
@Mailo: I have edited my answer and showed you how to pass it from inside. Hope it helps
0

Here is a solution to read the content of a text file (small) where parts are separated by an empty line :

# First read the file as one string
 $a = Get-Content 'D:\temp\test.txt' -raw

 # Get your arrays (here on the fact that the separator is an empty line
 $b = $a -split '\n\r'

 # Use array One
 foreach ($file in $($b[0] -split '\n'))
 {
 }

 # You can use $b[1] for file two etc...

2 Comments

My Arrays are separated by # only the entries are separated by empy line
This is not what you give in the sample ? Can you edit your sample I will edit my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.