PowerShell : automating commands for files in folder

 I had a requirement where I need to take a list of files in a folder, apply certain action to those files in this case it was applying a code sign certificate with a timestamp URL, that certificate was stored on a hardware key

That might sound like it made it complicated but that only requires one commands to complete the action with relevant software installed, so to start with I was getting one or two files, but at some point there will be I predict a large number of files added to that folder, The folder structure was pretty simple we had two folders one called “pending-sign” and the other called “signed-files”

#Get files in a folder and run the command against each file in the folder
$files = Get-ChildItem \\smbshare\msix\pending-sign
foreach ($file in $files){
signtool.exe sign /tr http://timestamp.entrust.net/rfc3161ts2 /td sha256 /fd sha256 $file.FullName
#Move the file to a new folder after the command has run
Move-Item –Path \smbshare\msix\pending-sign\$file -Destination \smbshare\msix\signed-files
}

If you don’t need to move files to a different folder then just comment out the last section of the script

After I completed the scripts I then found lots of other ways I could use this applying everything to files in a folder by changing the command used that’s run against them all, scripts are helpful if you have a lot of actions to do and you don’t want to manually do the steps which is why I use the script to do it because it takes the human error element out of the actions, scrips only do what you’re told to do so when you need a consistent approach it’s the way forward

Previous Post Next Post

Ω†Ω…ΩˆΨ°Ψ¬ Ψ§Ω„Ψ§ΨͺΨ΅Ψ§Ω„