0

I need some help with a powershell script to exclude certain folders and files.

I have it written so far that it ignores shortcuts and a folder called data, but when the delete command runs, it also deletes all of the content's of the Data folder on the users desktop.

This is the essential part of what I have

$exclude = ('*.lnk', "*data*")
{Get-ChildItem -Path ([environment]::GetFolderPath("Desktop")) -Exclude $exclude -Recurse | Remove-Item -Force -Recurse}

How can I best achieve what I want, this is to have the script run at each startup, remove all desktop content apart from shortcuts and the Data folder+contents. The contents could be anything from exe to mp3 and I do not want to just exclude MP3 from deletion as I may well want them removed from other folders that have been placed on the desktop.

regards

arco444
  • 20,737
  • 10
  • 61
  • 64
Marky
  • 13
  • 1
  • 5
  • possible duplicate of [Unable to exclude directory using Get-ChildItem -Exclude parameter in Powershell](http://stackoverflow.com/questions/19842754/unable-to-exclude-directory-using-get-childitem-exclude-parameter-in-powershell) – arco444 Dec 01 '14 at 12:13
  • I did look at that but using that code I get the following error.Where-Object : Cannot bind argument to parameter 'FilterScript' because it is n ull. At C:\cleanup.ps1:36 char:90 + 1{Get-ChildItem -Path ([environment]::GetFolderPath("Desktop" )) -Exclude $exclude | ? <<<< $_.fullname -notmatch "\\Data\\?" -Recurse | Remove-Item -Force} + CategoryInfo : InvalidData: (:) [Where-Object], ParameterBindin gValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M icrosoft.PowerShell.Commands.WhereObjectCommand – Marky Dec 01 '14 at 12:19
  • It could be I have the command formatted incorrectly? This is what I tried...{Get-ChildItem -Path ([environment]::GetFolderPath("Desktop")) -Recurse -Exclude $exclude | ? {$_.fullname -notmatch "\\Data\\?"} Remove-Item -Force -Recurse} – Marky Dec 01 '14 at 12:42
  • try `... | ? {$_.fullname -notmatch "\\Data\\?"} | Remove-Item -Force -Recurse}` – arco444 Dec 01 '14 at 12:45
  • Many thanks, doing it that way worked :) – Marky Dec 01 '14 at 12:58

0 Answers0