none
Remove Item question RRS feed

  • Question

  • I have the following script written to list all the user directories on selected Terminal Servers.

    It outputs them into a grid view, but what I want to be able to do is select the name from the grid view and have the script then delete the folder.

    Let's say I specify the server TS-1 and it brings back the list of users. I'll use Fred as an example.

    The list will have it showing as fred 09/20/2014 08:01:07

    When I click fred in the gridview I get an error of Remove-Item : Cannot find drive. A drive with the name 'fred 09/20/2014 08' does not exist.

    I know I'm missing a minor part of the script, but I can't seem to find what I'm looking for when researching it. I'm assuming it's looking for the literal path of the folder I want to delete.

    #Prompt for a computer to connect to  
    $computer = Read-Host "Please enter a computer name"
    gci \\$computer\c$\Users | % {"$_ $($_.LastWriteTime)"} |
            Sort-object name |
              Out-GridView -PassThru | select |
              Remove-Item

    Monday, September 22, 2014 6:25 PM

Answers

  • Remove-Item behave poorly.  DO it this way.

    No need to sort.  It will be sorted on the first column specified.

     dir *.txt|
         where{$_.LastWriteTIme -lt [datetime]::Today.AddDays(-5)} |
         select Name, fullname,lastwritetime |
         out-gridview -PassThru |
         ForEach{ Remove-Item $_.FullName -WhatIf }


    ¯\_(ツ)_/¯

    Monday, September 22, 2014 10:18 PM

All replies

  • gci \\$computer\c$\Users | 
         %{$_.LastWriteTime -lt [datetime]::Today} |
         Sort-object name |
         Out-GridView -PassThru |
         Remove-Item


    ¯\_(ツ)_/¯

    Monday, September 22, 2014 10:07 PM
  • Remove-Item behave poorly.  DO it this way.

    No need to sort.  It will be sorted on the first column specified.

     dir *.txt|
         where{$_.LastWriteTIme -lt [datetime]::Today.AddDays(-5)} |
         select Name, fullname,lastwritetime |
         out-gridview -PassThru |
         ForEach{ Remove-Item $_.FullName -WhatIf }


    ¯\_(ツ)_/¯

    Monday, September 22, 2014 10:18 PM
  • The second option works great, unless there are temp files open, which is something I'll work on. 

    Thanks JRV. 

    Tuesday, September 23, 2014 12:13 PM
  • dir *.* -exclude *.tmp


    ¯\_(ツ)_/¯

    Tuesday, September 23, 2014 12:17 PM
  • I'm not exactly sure where the dir *.* -exclude *.tmp statement belongs. 

    Tuesday, September 23, 2014 12:52 PM
  • gci c:\folder\* -Exclude *.tmp

    ¯\_(ツ)_/¯

    Tuesday, September 23, 2014 12:54 PM
  • Ah, thank you kindly. 
    Tuesday, September 23, 2014 12:56 PM