I am trying to write a script that will parse a directory, find the 3 newest files of a given extension among other files with the same name but different extension and then pass the output to a command line program.
So far I have:
$file = dir test -Exclude *.md5 | sort -prop LastWriteTime | select fullname -last 3
\path\program argumentA $file[0] argumentB "argumentC"
\path\program argumentA $file[1] argumentB "argumentC"
\path\program argumentA $file[2] argumentB "argumentC"
Not sure if it matters a whole lot but argument2 needs to be in quotes for the program to use it. I will eventually either have the output written to a file or, preferably use the output in another line of code described in the bonus question.
The problem I am having is that when I use $file[0] as an argument nothing happens. When I use the file name stored in $file[0] instead of using the variable, it runs, but does not pass argumentB or argumentC. If I run it from cmd I get the proper output.
My questions are:
How do I get the program to recognize a variable?
How do I get the program to recognize the other arguments?
Bonus question:
The program will output a list of files. How do I delete everything in the given folder except for the files listed in the output of the program?
Try
I find that I often have to wrap things in “magical” dollar sign brackets to get them working in PowerShell.