[Parameter(Mandatory=$False)]
[string]$EndDate = Get-Date
I would not recommend formatting it, as this turns it from type DateTimeto data type String, which leads to problems in yourWhere-Object
: , [string]. AFAIK Where-Object, DateTime ( PowerShell $EndDate DateTime...).
. 4c74356b41 , , , !
param (
[Parameter(Mandatory=$True)]
[string]$Path,
[Parameter(Mandatory=$True)]
[string]$targetDir,
[Parameter(Mandatory=$True)]
[string]$BeginDate,
[Parameter(Mandatory=$False)]
[string]$EndDate = (Get-Date),
[switch]$force
)
try{
[datetime]$BeginDate = $BeginDate
}catch{
Write-Output "$BeginDate is not a valid datetime"
}
try{
[datetime]$EndDate = $EndDate
}catch{
Write-Output "$EndDate is not a valid datetime"
}
Get-ChildItem -Path $Path -Recurse | Where-Object {$_.LastWriteTime -gt $BeginDate -and $_.LastWriteTime -lt $EndDate }| cp -Destination $targetDir -Force