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?

Mailo
  • 11
  • 2

3 Answers3

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")
)
Community
  • 1
  • 1
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • 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 Feb 09 '17 at 06:23
  • @Mailo: Yes, you have to call like that. THe link which I have given in that you can see some examples also for that – Ranadip Dutta Feb 09 '17 at 06:31
  • 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 Feb 09 '17 at 07:03
  • @Mailo: I have edited my answer and showed you how to pass it from inside. Hope it helps – Ranadip Dutta Feb 09 '17 at 07:59
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

Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • 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..." ? – Mailo Feb 09 '17 at 06:38
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...
JPBlanc
  • 70,406
  • 17
  • 130
  • 175