Tag Archives: powershell tutorial

How to delete files older than 30 days automatically using PowerShell

Delete files older than 30 days:

In many scenario we will store large number of non-important files on a different location, and its very difficult to delete those huge files old files monthly wise, to handle such scenario we are here going to use PowerShell and Task Scheduler to monitor and clean up files from any folder that are older than a specified number of days.

In this post, you’ll learn the steps to automatically delete files that haven’t been modified in the last month or any number of days you specify on Windows. The below scripts will delete the files that haven’t been modified in the last 30 days.

#################################################################
#Project : How to delete the old file automatically using Powershell
#Developer : Thiyagu S (dotnet-helpers.com)
#Tools : PowerShell 5.1.15063.1155 
#E-Mail : mail2thiyaguji@gmail.com 
##################################################################
Get-ChildItem –Path "C:\PowerShell\OriginalFolder" -Recurse | 
Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item

In the above command remember to change “C:\path\to\folder” specifying the path to the folder that you want to delete files, and change -30 to select files with a last modified date.

How to use Task Scheduler to delete files older than 30 days automatically using Powershell:

In above code we have created Powershell script for deleting the files older than 30 day, but our scenario is want to delete automatically, Most we will use the graphic interface of Taskschd.msc console to create Windows Task Scheduler jobs and Building a single scheduled task via the GUI task scheduler might not be a big deal. But if you find yourself creating scheduled tasks repeatedly it might be a good idea to use a method that scales better than a GUI like PowerShell. However, in various scripts and automated jobs, it is much more convenient to use the PowerShell features to create scheduled tasks. In this below link, we have detail description about how to create new Windows Scheduler tasks using PowerShell.

.

Creating a scheduled task with PowerShell

Most we will use the graphic interface of Taskschd.msc console to create Windows Task Scheduler jobs and Building a single scheduled task via the GUI task scheduler might not be a big deal. But if you find yourself creating scheduled tasks repeatedly it might be a good idea to use a method that scales better than a GUI like PowerShell.  However, in various scripts and automated jobs, it is much more convenient to use the PowerShell features to create scheduled tasks. In this article, we will discuss about how to create new Windows Scheduler tasks using PowerShell.

PowerShell v4 introduced a ScheduledTasks module that greatly simplified the times of creating Task Scheduler COM objects to build scheduled tasks from the command line. To create Task Scheduler, we can utlize the below setting.

Example: Passing PS file as argument

# Specify the trigger settings
$trigger= New-ScheduledTaskTrigger -At 10:00am –Daily
# Specify what program to run and with its parameter
$action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "C:\dotnet-helpers\TestScript.ps1"
# Specify the name of the task
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "EventLog" -Description "Application Logs"

Example: Passing scrip as argument

# In argument section - The script will get the evetlog and save in the excel in specific location.
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -command "& {get-eventlog -logname Application -After ((get-date).AddDays(-2)) | Export-Csv -Path c:\dotnet-helpers\eventlog.csv -Force -NoTypeInformation}"' 
$trigger = New-ScheduledTaskTrigger -Daily -At 7:41PM
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "EventLog" -Description "Application Logs"

OUTPUT

Explanation

New-ScheduledTaskAction

The New-ScheduledTaskAction cmdlet creates an object that contains the definition of a scheduled task action. A scheduled task action represents a command that a task executes when Task Scheduler runs the task.

New-ScheduledTaskTrigger

The New-ScheduledTaskTrigger cmdlet creates and returns a new scheduled task trigger object.

New-ScheduledTaskPrincipal

Creates an object that contains a scheduled task principal. Using this we can registers a scheduled task that will run as the Local Service account or scheduled task that runs under logged-in members of the Administrators user group that has the highest privileges.

New-ScheduledTaskSettingsSet

The New-ScheduledTaskSettingsSet cmdlet creates an object that contains scheduled task settings. Each scheduled task has one set of task settings.

New-ScheduledTask

The New-ScheduledTask cmdlet creates an object that contains the definition of a scheduled task. New-ScheduledTask does not automatically register the object with the Task Scheduler service.

Register-ScheduledTask

The Register-ScheduledTask cmdlet registers a scheduled task definition on a local computer.

 

What do you think?

I hope you have idea of how to create Task in Window task scheduler using Powershell . I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.

Getting Help Information

The PowerShell includes detailed Help topics that explain PowerShell concepts. There are also Help topics for each cmdlet and provider and Help topics for many functions and scripts.

Getting Help for Cmdlets

To get Help about Windows PowerShell cmdlets, use the Get-Help cmdlet. Here let we see some of the cmdlets help.

To get a list of all the cmdlet Help topics in your session use below,

get-help -category cmdlet

OUTPUT

If we want to display one page of each Help topic at a time, then we need to use the help function or its alias man. For example, to display Help for the Get-ChildItem cmdlet, type

Man Get-ChildItem/Help Get-Childitem

If we need to known about all content in the Help topic, below cmdlet will help us with help of FULL keyword

get-help get-childitem -full

Getting Help About Providers

To get Help for a provider, type “Get-Help” followed by the provider name. For example, to get Help for the Registry provider, then type like below

get-help registry

To get all the list of provide use below

get-help -category provider

Getting Help About Functions and Scripts

We also use the help topics for many scripts and functions in Windows PowerShell. To display the Help for a function, type “get-help” followed by the function name.

get-help disable-psremoting

Getting Help Online

If we want to find the help online through the powershell then its can be acheive by the below command.

get-help <command-name> -online

For example, to get the online version of the Help topic about the Get-ChildItem cmdlet, type:+

get-help get-childitem -online

 

What’s New With PowerShell

Windows PowerShell® 5.0 includes significant new features that extend its use, improve its usability, and allow you to control & manage Windows-based environments more easily. Windows Server 2012 R2 introduced the notion of Desired State Configuration, PowerShell 5.0 advances this concept through OneGet.

  • OneGet is a new module which way to search and install hundreds of software packages from the repository and this is known Chocolatey repository.
  • It is a package manager for Windows. It’s mission to be a framework for installing applications and tools that you need for a Windows computer.
  • It uses PowerShell to deliver packages built on the NuGet infrastructure.

Flow of using repository

  • List and manage software repositories for packages that can be installed.
  • Search and filter your repositories.
  • Install/uninstall packages from windows using the repositories.
  • Get started with the command: Import-Module -Name OneGet.

Installing Windows PowerShell in window’s 8.1

  • Windows PowerShell 5.0 is installed by default on Windows Server 2016 Technical Preview and Windows 10. 
  • To install Windows PowerShell 5.0 on Windows Server 2012 R2/Windows 8.1 Enterprise/Windows 8.1 Pro we need to download and install Windows Management Framework 5.0 here  
  • Don’t forgot to read the download details and meet all system requirements before starting your installation.

Windows Management Framework 5.0 Contains?

  • Contains updates for Windows PowerShell and Windows PowerShell ISE.
  • Package Management cmdlets.
  • Network Switch cmdlets.
  • Windows PowerShell Desired State Configuration (DSC)

To uninstall Windows Management Framework 5.0:

Navigate to the Control Panel\Programs\Programs and Features\Uninstall a program. You are looking for Windows Management Framework 5.0.