Jump to content

Powershell moving and deleting

ziogref

So below is a powershell script I wrote (first time using Powershell) to delete files, but everytime i run it it fails to delete, nothing gets deleted, all i get is the same error message for every single file.

Quote

cd C:\test\dest
Get-ChildItem -include *sample*,*.rar*,*.r00*,*r01*,*.r02*,*.r03*,*.r04*,*.r05*,*.r06*,*.r07*,*.r08*,*.r09*,*.r10*,*.r11*,*.r12*,*.r13*,*.r14*,*.r15*,*.r16*,*.r17*,*.r18*,*.r19*,*.r20*,*.r21*,*.r22*,*.r23*,*.r24*,*.r25*,*.r26*,*.r27*,*.r28*,*.r29*,*.r30*,*.r31*,*.r32*,*.r33*,*.r34*,*.r35*,*.r36*,*.r37*,*.r38*,*.r39*,*.r40*,*.r41*,*.r42*,*.r43*,*.r44*,*.r45*,*.r46*,*.r47*,*.r48*,*.r49*,*.r50*,*.r51*,*.r52*,*.r53*,*.r54*,*.r55*,*.r56*,*.r57*,*.r58*,*.r59*,*.r60*,*.r61*,*.r62*,*.r63*,*.r64*,*.r65*,*.r66*,*.r67*,*.r68*,*.r69*,*.r70*,*.r71*,*.r72*,*.r73*,*.r74*,*.r75*,*.r76*,*.r77*,*.r78*,*.r79*,*.r80*,*.r81*,*.r82*,*.r83*,*.r84*,*.r85*,*.r86*,*.r87*,*.r88*,*.r89*,*.r90*,*.r91*,*.r92*,*.r93*,*.r94*,*.r95*,*.r96*,*.r97*,*.r98*,*.r99* -recurse | foreach ($_) {Remove-Item $_.FullName}
#delete's unwated files

it runs but gives the following error message on all the files

Quote

RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand

 

Link to comment
Share on other sites

Link to post
Share on other sites

Where are the files located and is Powershell being run as admin?

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

Googled and found:

  1. add '-force' switch.
  2. Enable running unsigned scripts by entering: set-executionpolicy remotesigned OR
  3. Run the command as administrator if you only need to run that script once
Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, DeadEyePsycho said:

Where are the files located and is Powershell being run as admin?

In that script above it does say C drive, but I change the directory to G:\scripttemp then run it (so ignore the C drive stuff) The only user on the server is my self, and yes im running Powershell as Administrator

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, MeshFile said:

Googled and found:

  1. add '-force' switch.
  2. Enable running unsigned scripts by entering: set-executionpolicy remotesigned OR
  3. Run the command as administrator if you only need to run that script once

Added the force switch still failed, I have enabled set-executionpolicy remotesigned also im copying the command into powershell so its not running from a powershell file.

4 minutes ago, Mixchew said:

This is probably a different issue but it appears you are missing a "." at *r01*

try adding the . at *r01* not that it should make a difference.

Link to comment
Share on other sites

Link to post
Share on other sites

this is what im getting, also I check the permissions on the file, it is mine and i have full access

Untitled (1).png

Link to comment
Share on other sites

Link to post
Share on other sites

It runs on my windows 8.1 and windows 2008 server just fine.

 

What version of Windows server and what changes you've made from the vanilla install?

 

Why not just pipe the output of Get-ChildItem to Remove-Item so it becomes this?

Get-ChildItem * -include *.ini -recurse | Remove-Item

From: http://serverfault.com/questions/199921/force-remove-files-and-directories-in-powershell-fails-sometimes-but-not-always

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×