Most scripts deal with more than one thing—lists of servers,
lists of files, lookup codes, and more. To enable this, PowerShell
supports many features to help you through both its language features and
utility cmdlets.
PowerShell makes working with arrays and lists much like working
with other data types: you can easily create an array or list and then add
or remove elements from it. You can just as easily sort it, search it, or
combine it with another array. When you want to store a mapping between
one piece of data and another, a hashtable fulfills that need
perfectly.
Create an Array or List of Items
Problem
You want to create an array or list of items.
Solution
To create an array that holds a given set of
items, separate those items with commas:
To create an array of a specific type, use a strongly typed
collection:
PS > $list = New-Object Collections.Generic.List[Int]
PS > $list.Add(10)
PS > $list.Add("Hello")
Cannot convert argument "0", with value: "Hello", for "Add" to type "System
.Int32": "Cannot convert value "Hello" to type "System.Int32". Error:
"Input string was not in a correct format.""
To store the output of a command that generates a list, use
variable assignment:
If you want to copy newer (recent) files first, you could use the Robocopy /MAXAGE:n and /MINAGE:n command line options.
You will have to run Robocopy 2 or more times (depending on how much
you want to control the file copy order by the "age" of the files).
Here is information about the /MAXAGE:n and /MINAGE:n command line options:
/MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date.
/MINAGE:n :: MINimum file AGE - exclude files newer than n days/date.
(If n < 1900 then n = n days, else n = YYYYMMDD date).
In the simplest case for example, you could first copy files that are
AT-MOST 1 day old, then when that is finished, copy all files that are
AT-LEAST 1 day old. Here are the 2 Robocopy command lines for this
(first) example:
First run of Robocopy:
robocopy "C:\source\path" "C:\dest\path" /S /COPY:DAT /DCOPY:T /MAXAGE:1
Second run of Robocopy:
robocopy "C:\source\path" "C:\dest\path" /S /COPY:DAT /DCOPY:T /MINAGE:1
If you want more control than that, you can combine the 2 options. For example:
First copy all files that are AT-MOST, 1 day old
Then copy files that are AT-LEAST 1 day old, and AT-MOST 2 days old
Then copy files that are AT-LEAST 2 days old, and AT-MOST 3 days old
Then copy files that are AT-LEAST 3 days old, and AT-MOST 5 days old
Then copy files that are AT-LEAST 5 days old (all remaining filea)
Here are the Robocopy command lines for this (second) example: