I’m a beginner in Powershell and am trying to construct scripts to assist with learning. I’ve been using the Microsoft Powershell site which has been a great help with learning cmdlets, but I’m now at the stage where I’m trying to combine commands in order to achieve multiple actions in a script (still veerrrryy basic stuff :-).
I have the following:
A folder called Pics, containing .jpg files
A folder called Archives, containing .zip files
A .txt file containing the filenames of the Archives directory, with ".zip" removed
I am trying to achieve either of the following, but mainly point 1:
Rename all items in the Pics folder using the list contained in the .txt file
Rename all items in the Pics folder using the filenames in the Archive folder, removing ".zip" in the process
This is assuming the files in the pics folder are already named so they appear in the same order as the contents of the Archive folder, and filename list in the .txt file.
I’ve not been able to find a working combination of Get-Content, Get-ChildItem and Rename-Item (possibly by losing the original value of $_ when piping into another command?) so wondering if I would need to achieve this using variables and loops or is there a way it can be achieved using basic cmdlets?
It would be better if you would post what you’ve tried and what is not working. First hint, you don’t need a text file. There is a property BaseName that that has the base file name with no extension, so you can just use Archive file collection:
Next, there are a lot of logic errors you are going to deal with as there are a lot of assumptions.
What if there are there more files in Pic than there are in Archive or vice versa?
What if there are no files in Pic or Archive?
What if the order is based on datetime created and doesn't match?
If you want to do it simply learn, it can be done, but assuming what is in the directories would quickly break things in a production script. Please share what you’ve tried.
V3 introduced the PipelineVariable that can help when nesting…
'bits','abc','dhcp' |
ForEach-Object { $_ } -PipelineVariable name |
Get-Service -PipelineVariable service -ErrorAction SilentlyContinue |
& { process{ "The service $name is present" }}
name comes directly from the string array.
Note: The name can be extracted from the object returned from get-service, but it shows the technique.