Using a powershell script to rename a file by stripping the appended date

The problem is simple.  I want to display an embedded pdf on a web page.  My Sonicwall reports for example only creates reports with the date appended to the file name.  I want to accomplish the following:

  • Remove yesterday’s file (date already stripped)
  • Rename today’s file which contains the date in the name
  • Copy the renamed file with the date stripped to a network server
  • Set up a scheduled task

Here is a sample powershell script:

Remove-Item c:local_foldertest.txt
rename-item -path c:local_foldertest*.txt -newname test.txt
Copy-Item -Path c:docstest.txt -Destination \network_serverdestination foldertest.txt

That’s your script.

Note: Please make sure that your local environment is set to execute Powershell scripts, instead of only allowing interactive commands to be run in the Powershell environment.  To accomplish this, type the following at the Powershell command prompt:

set-executionpolicy RemoteSigned

This will allow the system to run Powershell scripts that are created locally (Remote Powershell scripts that may be downloaded must be signed).

To create a scheduled task:

Now to run the script outside of its Powershell environment you type a command similar to the following:

powershell -command "& 'MyScript.ps1' "

Just put the above command into a .bat or .cmd file and schedule it like you would normally schedule a script to be run with Windows task scheduler. Happy coding!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.