Posts Tagged ‘scheduled’
Automatically Delete Older Folder Contents with Powershell on Windows 7
The aim of this guide is to create a folder whose content is deleted if the contained files have not been accessed longer than a specific time (this process is applied to single files, not the whole folder). I will choose two weeks for demonstration purposes (= 14 days). Such a folder can be used as temporary folder of any kind, be it for downloaded files/installers or to just keep the desktop clutter-free.
This can be achieved with a combination of PowerShell script and Windows task scheduler. The folder that I will use for this will be C:\Users\howtoforge\Desktop\Temp and is located on my desktop for easy access. To keep order to it, create another folder for your custom scripts if you haven’t already got one, mine will be C:\Scripts.
Open a new instance of notepad and save it in your scripts folder as delete_temp.ps1. .ps1 is the file extension for PowerShell scripts. Now enter following into the script:
cd “C:\Users\howtoforge\Desktop\Temp”;
Get-Childitem | Foreach-Object {if ($_.LastAccessTime -le (get-date).adddays(-14)) {remove-item -recurse -force $_}};
Save the script again. What it does: the script changes into the directory that we want to observe, looks at its items and then deletes every one whose last access time is older than 14 days recursively (it only looks at the items directly placed in the folder, not at subdirectories). The time interval is specified in the adddays attribute of the get-date function here (which can also be addmonths, addhours, etc…) and is a negative number to actually subtract the number of days from the present date. You can change it to your likings.
The script being ready, you have to configure PowerShell to enable calling scripts – therefore open an elevated command line (search the menu for cmd, right-click and select Run as administrator). Open PowerShell by entering
powershell
Afterwards, enter
Set-ExecutionPolicy RemoteSigned
to enable calling scripts. Now you can test your script by right-clicking it and selecting Run with PowerShell. If nothing goes wrong (no red text in the flashing window), proceed to schedule the task, otherwise check your script for errors.
To schedule the task, open Control Panel > System and Security > Administrative Tools > Schedule Tasks. On the left pane, select Task Scheduler Library, then right-click the central task-list and select Create New Task.
On the General tab, give the task a name and a description. Furthermore, choose your version of Windows and optionally choose to run it with highest privileges.
The Trigger tab defines what will call the script – hit New and choose one or more of the various possibilities and events. I choose to run the script when the machine goes idle, since the script will blink up in a PowerShell window when called, and I don’t want that to disturb my work (although it’s really only a split second if you don’t delete several GB of files).
On the Actions tab you define what to do – hit New again. Now don’t enter the actual script as program to run – this goes to the Add arguments line (enter the full path here). What you need to do is to call the PowerShell executable with the script as an argument. I use PowerShell 1.0 which is located in C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe – enter this path into the Program/script line and hit OK.
Now configure the next two tabs for your needs and hit OK again to create the task.
The selected folder will then be scanned for files that haven’t been accessed for longer than the given period every time the task triggers.
Timed Shutdown on Windows
You can time a shutdown on your computer easily by creating a short vbscript that contains either only the shutdown command, or a shutdown command with an integrated second timer. If you do not add a timer, you can also use Windows’ Schedule Tasks function to have the computer shutdown at a specific time. The vbscript to shutdown looks like this:
Set objShell = wscript.CreateObject(“wscript.Shell”)
objShell.run(“shutdown /s”)
If you want a timer, add
/t xxx
to the quoted expression in the second line, where x is the number of seconds to count down. Enter this code into a notepad and save the file as .vbs. Do not save it as text file.
Shortcuts for creating System Restore Points on Windows 7
On every Windows system able to create system restore points that undo any configuration changes made after their creation, there is also the possibility to create simple quick-link icons, consisting of a few line of code, that enable you to create restore points with a double-click. On Windows XP this is achieved with only two lines of code. Learn in this post, how it is done. In Windows Vista and 7 however, there are a few problems that stand in the way of our (automatic) one-click system restore point. First one is, that the script we need to run to create system restore points can only be run with administrative powers, so we need a way to get those. Second is the annoying User Account Control that asks as if we really want to run that script. This would not be such a great deal, but if you are the kind to create restoration points quite often or after a time schedule, the UAC may become a great pain.
To start with the administrative rights, there are more than one way to get those. The probably least complex one is to add the Run as administrator option to the menu appearing upon right-clicking the script:
This is done by adding the appropriate keys to the Windows Registry. To open that, open Run… by entering run into the Windows search bar in the main menu and clicking on the program. Type in regedit into Run and the Windows Registry will open. It consists of two columns, one on the left, containing the key directories, and one on the left, showing the keys’ values. Take on the left column and browse the HKEY_CLASSES_ROOT\VBSFile\Shell directory. Right-click it and select New > Key. Name it Runas and leave its values as they are (there is only one). Now right-click the Runas key and again select New > Key. Name the newly created key Command and leave the values as they are. Right-click the Command key and select Export…. Give it a name and save it somewhere you will find it. Go to the directive you saved it and open it with notepad. Erase all of its contents and paste this:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\VBSFile\Shell\Runas\Command]
@=hex(2):22,00,25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,57,00,\
53,00,63,00,72,00,69,00,70,00,74,00,2e,00,65,00,78,00,65,00,22,00,20,00,22,\
00,25,00,31,00,22,00,20,00,25,00,2a,00,00,00
Save the file and double-click to merge it with the registry. The Open as administrator option should now be available to all Visual Basic Scripts (you can use the one from the linked XP tutorial with this option enabled).
However, this option neither is automatic nor does it circumvent the UAC. An option that enables creating a restoration point on double-click (but still, without disabling UAC) is to alter the script that you use for this. Open a new notepad (it has to be notepad) and paste the following code:
if wscript.arguments.count = 0 then
set objshell = createobject(“shell.application”)
objshell.shellexecute “wscript.exe”, wScript.scriptfullname & ” run”, , “runas”, vbnormalfocus
else
getobject(“winmgmts:\\.\root\default:systemrestore”).createrestorepoint “Automatic Restore Point (Win7 Script)”, 0, 100
end if
Save the script as *.vbs and make sure to select All files from the dropdown menu before saving! With this, you have enabled administrative rights beforehand every time you run the script. Now there still is a slightly circumstantial method to also disable the UAC prompt. To achieve this, you need to have a desktop shortcut pointing to a scheduled task that runs the script with highest privileges grantable. To access scheduled tasks, browse Menu > Control Panel > System > Administrative Tools > Scheduled Tasks. Create a new one without any trigger, executing your script (be sure to remember the task’s name, you will need it once more) and check the Run with highest privileges box on the first tab.
Now right-click your desktop and create a new shortcut. Let it point to following location (enter your own task-name in the last option):
C:\Windows\System32\schtasks.exe /run /tn “EnterYourTaskNameHere”
Upon opening, it should execute the script without asking for anything.
Create System Restore Points in Win XP automatically
Since older system restore points are deleted after some time, it is always good to create some new regularly. But to always have to browse through the countless menus day by day can be really annoying, that is why we will use Windows’ Notepad and Scheduled Tasks functions to ease our lives.
First we create a short script that creates system restore points in our notepad, therefore open one and type in these two lines:
Set auto_rp = getobject(“winmgmts:\\.\root\default:Systemrestore”)
auto_sys_rp = auto_rp.createrestorepoint (“Automatic System Restore Point”, 0, 100)
When saving, make sure to not save it as text document but select All Files from the Save as type dropdown menu and name it something like automatic_sysrp.vbs. vbs is the type of file here, make sure you have it correct, since otherwise the script will not be executable.
Now that you have created the script, you can doubleclick it to create a system restore point. But to have it done automatically, we are going to use Windows’ Scheduled Tasks function. Therefore, enter Start > Control Panel > Performance and Maintenance > Scheduled Tasks. Select File > New > Scheduled Task from the control bar. Give it a name, rightclick it and select Properties.
In the opened window, browse the location of the vbs script you just created and go to the Schedule tab. Enter a time when the restore point shall be created and click on Apply when you are finished. A new system restore point will now be created at the time you specified or on doubleclick upon the script.



