1

I have this Powershell script:

Get-ChildItem –Path "D:\Downloads" -Recurse | 
Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))} | Remove-Item -Recurse -Force -Confirm:$false

If I paste it on a Powershell Window, it work's, but if i try to execute the .ps1 file with the script, a blank Powershell Window open 1 second (because I make that the .ps1 files were opened by Windows Powershell) and next it closes, but the script doesn't work. Only work If I manually open a Powershell Window and paste it the script. The file has privilegies for all the users...

UPDATE:

If I run as bat file, it shows me an error on the code.

enter image description here

7
  • 1
    for me your script work fine and delete files. Check your execution policy may be it's problem, and one more time check permission on folder. Commented Mar 3, 2020 at 8:30
  • I checked, giving total control to all the users on the D:/Downloads folder and on the file itself, but still without working. I tried to move the file to another location and execute it too. I have another .ps1 file that clean the Recycle bin that it works opening the file and have the same privilegies, but this script don't work by this way... Commented Mar 3, 2020 at 8:54
  • I'm sure that you have already checked, but still ask. Is there a file older than 30 days? Commented Mar 3, 2020 at 8:56
  • "the script doesn't work" - what does it do that tells you it doesn't work? is there an error message? You could add "start-sleep -seconds 30" to the end of your script to give you a chance to read the screen before it closes. Commented Mar 3, 2020 at 9:22
  • I used the "start-sleep -seconds 30" and it still closes instantly. @Vlad, yes, there are a file older than 30 days. I tried the same file all time, if I use the script manually from a powershell Window the script work and delete it, if I run the file directly, it didn't delete it. Commented Mar 3, 2020 at 9:42

1 Answer 1

0

Solved, was a dash on the code, it wasn't like the others:

And it need to be:

-

Thanks.

Sign up to request clarification or add additional context in comments.

2 Comments

That's called an "em dash" (see en.wikipedia.org/wiki/Dash#Em_dash) - it often appears when you copy code snippets from a rich text editor (e.g. Microsoft Word, Email clients, or sometimes a web page) - they often automatically substitute the normal hyphen / minus sign for an Em Dash as you type because it looks better on a screen / printed page, but that breaks scripts that expect a regular hyphen.
@mclayton, good points, but note that PowerShell does allow (em dash) use in lieu of the ASCII-range - (hyphen-minus) - see this answer. While using a regular - is definitely advisable, as is preferring the use of ASCII-range quotation marks to their typographical counterparts, the only real problem here was one of character encoding - see the linked duplicate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.