Asked by:
Write a script for removing the content of a folder.

Question
-
Hello.
I have a folder in drive "C:" and I want to write a script for removing the contents of this folder every 24 hours. How can I do it?
Thank you.
Saturday, February 17, 2018 4:52 PM
All replies
-
try to
create a batch file example RemoveFolder.cmd
RD youfolder /s
md youfolder
and create a schedule task
Chris
Saturday, February 17, 2018 5:23 PM -
Get-Help About_Jobs
Get-Command *scheduled*
Regards kvprasoon
- Edited by PRASOON KARUNAN V Saturday, February 17, 2018 6:03 PM
Saturday, February 17, 2018 5:42 PM -
Use PowerShell in a scheduled task.
Remove-Item c:\yourfolder\* -Recurse
This can be scheduled with one line.
Command: powershell
Arguments: -Command "Remove-Item c:\yourfolder\* -Recurse"No batch file needed.
Be sure to use an account with access.
\_(ツ)_/
Saturday, February 17, 2018 5:43 PM -
You can also quickly create a PS scheduled job that runs as required and is easier to create and manage.
Register-ScheduledJob -ScriptBlock {Remove-Item D:\testjunk\* -Recurse} -Name CleanFolder -Trigger @{Frequency='Daily';At='3am'}
Or my favorite way:
$task = @{ ScriptBlock = { Remove-Item D:\testjunk\* -Recurse } Name = 'CleanFolder' Trigger = @{ Frequency = 'Daily'; At = '3am' } } Register-ScheduledJob @task
Any output is saved in a standard job object but the jobs are global to the account. Even after a reboot the job result remain until removed.
Use "Get-Job" to find your jobs.
\_(ツ)_/
- Edited by jrv Saturday, February 17, 2018 5:55 PM
- Proposed as answer by I.T Delinquent Monday, February 19, 2018 9:49 AM
Saturday, February 17, 2018 5:52 PM -
You can also quickly create a PS scheduled job that runs as required and is easier to create and manage.
Register-ScheduledJob -ScriptBlock {Remove-Item D:\testjunk\* -Recurse} -Name CleanFolder -Trigger @{Frequency='Daily';At='3am'}
Or my favorite way:
$task = @{ ScriptBlock = { Remove-Item D:\testjunk\* -Recurse } Name = 'CleanFolder' Trigger = @{ Frequency = 'Daily'; At = '3am' } } Register-ScheduledJob @task
Any output is saved in a standard job object but the jobs are global to the account. Even after a reboot the job result remain until removed.
Use "Get-Job" to find your jobs.
\_(ツ)_/
Thank you.
The "-Recurse" parameter mean remove all files in "testjunk" folder and not drive "D:" ?
Sunday, February 18, 2018 4:10 PM -
Hi,
It will remove everything under D:\testjunk but still keep the testjunk folder.
If you need further help, please feel free to let us know.
Best Regards,
Albert
Please remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Edited by Albert LingMicrosoft contingent staff Monday, February 19, 2018 11:17 AM
Monday, February 19, 2018 11:16 AM -
Hi,
I'm checking how the issue is going, was your issue resolved?
And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.
Appreciate for your feedback.
Best Regards,
AlbertPlease remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.comWednesday, February 21, 2018 5:00 AM -
Hi,
It tells me "The term 'register-scheduledjob' is not recognized as the name of a cmdlet".
It will remove everything under D:\testjunk but still keep the testjunk folder.
If you need further help, please feel free to let us know.
Best Regards,
Albert
Please remember to mark the replies as an answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.comMonday, March 5, 2018 11:22 AM -
Any idea?Sunday, April 8, 2018 8:52 AM
-
seems like you are running a very old version of PowerShell, may be version 2. We suggest you to upgrade it to the latest , I would suggest 5.1. PowerShell version 2.0 is going to get deprecated due to many security concerns.
The command you are trying to execute is available only from PowerShell Version 3.0 onwards.
Regards kvprasoon
Sunday, April 8, 2018 2:25 PM -
Just to advise on this, you can check your current version of Powershell with the command $PSVersionTable, which will make Prasoon's point easier to understand.
Steven Andrews
SharePoint Business Analyst: LiveNation Entertainment
Blog: baron72.wordpress.com
Twitter: Follow @backpackerd00d
My Wiki Articles: CodePlex Corner Series
Please remember to mark your question as "answered" if this solves (or helps) your problem.Monday, April 9, 2018 12:22 PM -
Just to advise on this, you can check your current version of Powershell with the command $PSVersionTable, which will make Prasoon's point easier to understand.
It is "2.0.50727.5420".
Steven Andrews
SharePoint Business Analyst: LiveNation Entertainment
Blog: baron72.wordpress.com
Twitter: Follow @backpackerd00d
My Wiki Articles: CodePlex Corner Series
Please remember to mark your question as "answered" if this solves (or helps) your problem.Monday, June 18, 2018 6:59 AM