The "dash" in front of Path is not a dash, it's some other character.
Powershell script is not running via ps1 file
Hi Team, I wrote a simple script to delete all the files older than X days from my folders without deleting my Folders obviously , i'm able to run it through a command line powershell directly as a command but when i'm calling my ps1 script file in powershell it's giving me errors, i have also recieved more errors earlier but now its just this.
Here is my script,
$logoutput=Get-ChildItem –Path "C:\TEST" -Recurse -File | Where-Object {($_.LastWriteTime -lt (Get-Date).AddMinutes(-1))}
Write-Output "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date) $logoutput" | Out-file C:\log.txt -append
Get-ChildItem –Path "C:\TEST" -Recurse -File | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item
It's in a file called cleanup.ps1 and when i'm running powershell -file "path to cleanup.ps1" it's giving below error.
I have done quite a lot but nothing helped.
"""''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Get-ChildItem : A parameter cannot be found that matches parameter name 'File'.
At C:\Users\sahmad\cleanup.ps1:4 char:42
+ Get-ChildItem –Path "C:\TEST" -Recurse -File | Where-Object {($_.La ...
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
3 answers
Sort by: Most helpful
-
-
MotoX80 34,346 Reputation points
2023-01-20T14:05:08.08+00:00 In your first reply you are mixing up the $i and $file variables.
In your second reply, you need to treat the $file variable as a collection of file objects.
Remove the -whatif when you are satisfied that the correct files are being selected and you actually want to delete them.
$Path = "C:\TEST" $Minutes = 1 # numeric not string $DelDate = (Get-Date).AddMinutes(-$Minutes) $DelDate # display the timestamp that we calculated $file = Get-ChildItem -Path $Path -Recurse -File | Where-Object { $_.LastWriteTime -lt $DelDate } foreach ($f in $file) { "[{0}] {1}" -f (Get-Date -Format 'MM/dd/yyyy HH:mm:ss'), $f.fullname | Out-File "C:\Users\sahmad\log.txt" -Append } if ($file) { # do we have any files? $file | Remove-Item -Force -whatif # pipe the collection of objects to remove-item }
-
Limitless Technology 44,381 Reputation points
2023-01-23T08:37:34.84+00:00 Hi. Thank you for your question and reaching out. I’d be more than happy to help you with your query.
I have linked an article below that may help solve your issue. Here is what the article has to say:
You have two options when running a script in the PowerShell console.
- Use the whole script's path, such as C:\TEMP\MyNotepadScript.ps1
- You can also use simply the script name from the folder containing the file: .\MyNotepadScript.ps1
REFERENCE: https://petri.com/how-to-write-and-run-a-powershell-script-file-on-windows-11/
If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.