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

  • 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. – Vad Mar 03 '20 at 08: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... – Pedro Corchero Murga Mar 03 '20 at 08:54
  • I'm sure that you have already checked, but still ask. Is there a file older than 30 days? – Vad Mar 03 '20 at 08: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. – mclayton Mar 03 '20 at 09: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. – Pedro Corchero Murga Mar 03 '20 at 09:42
  • I tried to run first as bat file, see the image, it shows me an error now. – Pedro Corchero Murga Mar 03 '20 at 10:03
  • In short: a `–` (en dash) instead of a regular `-` (hyphen-minus) is _not_ a problem in itself, as long as the script file uses a character encoding properly recognized by PowerShell. In the case at hand, the symptom implies that the `.ps1` file uses UTF-8 _without a BOM_, which _Windows PowerShell_ misinterprets as ANSI; PowerShell [Core] 6+, which defaults to UTF-8, wouldn't have that problem - see [this answer](https://stackoverflow.com/a/55053609/45375). – mklement0 Mar 03 '20 at 13:06

1 Answers1

0

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

And it need to be:

-

Thanks.

  • 1
    That's called an "em dash" (see https://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 Mar 03 '20 at 12:35
  • @mclayton, good points, but note that PowerShell _does_ allow `–` (em dash) use in lieu of the ASCII-range `-` (hyphen-minus) - see [this answer](https://stackoverflow.com/a/55053609/45375). 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. – mklement0 Mar 03 '20 at 13:03