locked
Remove-Item RRS feed

  • Question

  • Will Remove-Item work with CSV files to get multiple machine names and remove the files from the machines?

    I am using this code below that I wrote without success.  Also when this runs, PowerShell doesn't display any errors.  It actually looks like it completed successfully.

    Any help would be greatly appreciated.

    # Remove-Item with PowerShell
    Clear-Host
    $CSVFile ="C:\Users\User\Desktop\renamecomputers.csv"
    $List = Import-CSV $CSVfile -header machinename
    Foreach ($Machine in $List) {Remove-Item C:\users\public\desktop\*}


    • Edited by Mr. Haus Wednesday, August 9, 2017 11:04 PM
    Wednesday, August 9, 2017 11:03 PM

Answers

  • Import-CSV "C:\Users\MrHaus\Desktop\renamecomputers.csv" -header machinename | Foreach {
      Remove-Item "\\$($machine.machinename)\c$\users\public\desktop\*" -whatif
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by Mr. Haus Thursday, August 10, 2017 4:20 PM
    Thursday, August 10, 2017 3:43 PM

All replies

  • Not sure what you are asking.  re you removing machines or files?

    If you are an admin you can remove files using the admin share.

    https://en.wikipedia.org/wiki/Administrative_share


    \_(ツ)_/

    Wednesday, August 9, 2017 11:11 PM
  • Yeah, I'm with jrv here. 

    Use the administrative share:

    foreach ($machine in $List) {Remove-Item "\\$($machine)\c$\users\public\desktop\*" -whatif}

    Thursday, August 10, 2017 1:40 PM
  • Yeah, I'm with jrv here. 

    Use the administrative share:

    foreach ($machine in $List) {Remove-Item "\\$($machine)\c$\users\public\desktop\*" -whatif}

    No reason for $($machine) as it is not an object, can just be \\$machine\c$\...

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, August 10, 2017 1:43 PM
  • For some reason this did not work.  So I went to a simple 1 line command -

    PS C:\Windows\system32> remove-item \\phx-cts-001-251\C$\users\public\desktop\*

    and that worked fine.  The CSV file works well in another script to change the PC name.  I can't find the error, nor does PowerShell give me any errors with this script -

    # Remove-Item with PowerShell
    Clear-Host
    $CSVFile ="C:\Users\MrHaus\Desktop\renamecomputers.csv"
    $List = Import-CSV $CSVfile -header machinename
    foreach ($machine in $List) {Remove-Item "\\$machine\c$\users\public\desktop\*" -whatif}

    Thursday, August 10, 2017 3:27 PM
  • Thank you.
    Thursday, August 10, 2017 3:37 PM
  • Thank you.
    Thursday, August 10, 2017 3:38 PM
  • Import-CSV "C:\Users\MrHaus\Desktop\renamecomputers.csv" -header machinename | Foreach {
      Remove-Item "\\$($machine.machinename)\c$\users\public\desktop\*" -whatif
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by Mr. Haus Thursday, August 10, 2017 4:20 PM
    Thursday, August 10, 2017 3:43 PM
  • Hi,

    You may try the script below. -Whatif is there for testing without actually removing it. If you want to test to see what will be removed if you run the command, add -whatif

    Clear-Host
    $CSVFile ="C:\Users\MrHaus\Desktop\renamecomputers.csv"
    $List = Import-CSV $CSVfile -header machinename
    foreach ($machine in $List) {Remove-Item "\\$($machine.machinename)\c$\users\public\desktop\*"}

    Thursday, August 10, 2017 3:54 PM
  • This works wonderfully after I added the argument ($machine in $list) before the Remove-Item.  Thank you very much.
    Thursday, August 10, 2017 4:20 PM