Link to home
Start Free TrialLog in
Avatar of Alex
AlexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

powershell, again.... I should take up a new career

$Path = "$env:USERPROFILE\appdata\roaming\"

$Files="*.pdf","*.doc","*.dot","*.docx","*.docm","*.dotx","*.dotm","*.docb","*.xls","*.xlt","*.xlm","*.xltx","*.xlsm","*.xlsb","*.xla","*.pptx","*.pptm","*.potx","*.potm","*.ppsx","*.ppsm"

Get-childitem $Path -include $Files -recurse | foreach ($_) {remove-item $_.fullname}

Open in new window



Ok,

So this is meant to delete all files associated with the above file types, it doesn't. It also doesn't pull the array correctly, it only pulls *.PDF

I'm getting rather frustrated. Could someone help again please :(
SOLUTION
Avatar of Toni Uranjek
Toni Uranjek
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

It doesn't delete anything however there are no errors either!

It's running as admin

it's my profile

I'm getting a little hacked off tbh, it shouldn't be this difficult


User generated image
Avatar of Alex

ASKER

I've even tried wrapping the command in { } and that makes no difference...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

Nope they are still there and it's not doing anything with them, that was the first thing I checked.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Which version of Powershell are you using?

$PSVersionTable

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

I'm on powershell version 5
Avatar of Alex

ASKER

That has only removed the PDF files, would it be better to use a -or function between the file extensions?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
adding a * to your path makes your script work. remove the measure command block.. just adding the * makes it the fastest

measure-command {
  $Path = "$env:USERPROFILE\appdata\roaming\*"
 $Files="*.pdf","*.doc","*.dot","*.docx","*.docm","*.dotx","*.dotm","*.docb","*.xls","*.xlt","*.xlm","*.xltx","*.xlsm","*.xlsb","*.xla","*.pptx","*.pptm","*.potx","*.potm","*.ppsx","*.ppsm"
  Get-childitem $Path -include $Files -recurse 
}

Open in new window

Avatar of Alex

ASKER

Tony,

Yes, running the $files with the array and then $files on it's own shows the file types

David, your script didn't delete the files.

This is beyond stupid, I have no idea why this isn't working
Is array displayed horizontaly or verticaly?
David's script does not contain remove-item, it will delete files, when you will add remove-item. ;)
Avatar of Alex

ASKER

There has to be an issue with the file types

https://i.imgur.com/BYkfBKa.png
Avatar of Alex

ASKER

It's horizontal, each is on it's own line.

https://i.imgur.com/QBNCarB.png
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

which means

$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files="*.pdf","*.doc","*.dot","*.docx","*.docm","*.dotx","*.dotm","*.docb","*.xls","*.xlt","*.xlm","*.xltx","*.xlsm","*.xlsb","*.xla","*.pptx","*.pptm","*.potx","*.potm","*.ppsx","*.ppsm"
Get-childitem $Path -include $Files -recurse | Remove-Item

Open in new window


Should work, it doesn't.

I'm leaving this till tomorrow now anyway, it's getting me angry now
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

ok right i'm making progress
$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files="*.JPG"
$files | ForEach-Object { Get-ChildItem -Path $path -Filter $_ -Recurse | Remove-Item}

Open in new window


That removes all the JPG files in the folder

It's definitely the array!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

qlemo

I've just tried

$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files= "*.pdf,*.doc,*.dot,*.docx,*.docm,*.dotx,*.dotm,*.docb,*.xls,*.xlt,*.xlm,*.xltx,*.xlsm,*.xlsb,*.xla,*.pptx,*.pptm,*.potx,*.potm,*.ppsx,*.ppsm"
$files | ForEach-Object { Get-ChildItem -Path $path -Include $_  -Recurse | Remove-Item}

Open in new window



No luck
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Alex

ASKER

Doing them individually works fine.

Doing more than 1 fails and doesn't throw back an error

$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files= '*.xlsx'
$files | ForEach-Object { Get-ChildItem -Path $path -Include $_ -Recurse | Remove-Item}

Open in new window


Works great
Avatar of Alex

ASKER

RIGHT!

WE GOT IT GUYS, I LOVE YOU ALL SOOOO MUCH!

It was my variable missing something

$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files= "*.pdf","*.doc","*.dot","*.docx","*.docm","*.dotx","*.dotm","*.docb","*.xlsx","*.xls","*.xlt","*.xlm","*.xltx","*.xlsm","*.xlsb","*.xla","*.pptx","*.pptm","*.potx","*.potm","*.ppsx","*.ppsm"
$files | ForEach-Object { Get-ChildItem -Path $path -Include $_ -Recurse | Remove-Item}

Open in new window


It was the "*.xls","*.DOC" that I needed, it works so handing out points!
But as suggested, you do not need the loop here. The single Get-ChildItem ... -Include $Files works best.