I'm new to powershell and have only used script parameters of type string yet. Now I try to create a script which deletes backups which are older than a given time span, similar to this one:
Param(
[parameter(Mandatory=$true)]
[string] $BackupFolder,
[parameter(Mandatory=$true)]
[Timespan] $StorageDuration
)
$BackupFolder | ls -ad |? { (New-TimeSpan -Start $_.CreationTime -End Get-Date) -ge $StorageDuration} |% {$_|rm -Confirm:$false -Recurse}
Is there a way for a caller from the PS command line to pass a time span argument like e.g. 'New-TimeSpan -Days 7' ? I made a workaround where only the days are passed as an argument but I don't like this limitation and would prefer to pass the whole time span as a single parameter, possibly including days, minutes, hours and so on.
There are also two minor issues in my script which I don't understand. Maybe you can also answer these to help me improving my powershell knowledge:
MSDN says that d and ad are aliases for the Directory parameter. But when I use d instead of ad, I receive an error message saying that no argument for the parameter depth was specified. Did they mix something up there?
Why do I have to write a colon between -Confirm and $false? When I omit that, it tells me that no positional parameter takes the value false.