selecting items from an output

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Garegin Asatryan Garegin Asatryan 1 year, 8 months ago.

  • Author
    Posts
  • #22778
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    Hello

    I got a weird question. Can I use powershell in a way to select "items" from an output. say get-vm or get-disk. I know PS is built around objects, so obviously it understands that each line is a separate thing and not just a stream of text.
    my use case would be eg. select from the 10th to the 30th in the output and delete them. Just like in a file explorer.

  • #22779
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi Garegin,

    It's certainly possible. Here's an idea of how that type of thing can be done, only with files as an example.. The first illustration is just to show that there are currently five files in the directory, and the second shows how we get the files, select the range of items we want, and then remove them. I've deliberately done from the second element in the array (1), to the third to show that we can also remove items in the middle of a list.

    Set-Location D:\Temp\epguides
    Get-ChildItem
    
    
        Directory: D:\Temp\epguides
    
    
    Mode                LastWriteTime         Length Name                                                                                                                                                                                                                                                                               
    ----                -------------         ------ ----                                                                                                                                                                                                                                                                               
    -a----        2/15/2015  10:09 PM            309 epguides.css                                                                                                                                                                                                                                                                       
    -a----        2/15/2015  10:31 PM           3484 epguides.ps1                                                                                                                                                                                                                                                                       
    -a----        2/15/2015  10:09 PM          72174 jquery.js                                                                                                                                                                                                                                                                          
    -a----        2/15/2015  10:09 PM            429 manifest.json                                                                                                                                                                                                                                                                      
    -a----        2/15/2015  10:09 PM           3048 modify.js        
    
    PS 02/20/2015 06:53:24> Set-Location D:\Temp\epguides
    $files = Get-ChildItem
    $files[1..3] | Remove-Item -Force
    Get-ChildItem
    
    
    
        Directory: D:\Temp\epguides
    
    
    Mode                LastWriteTime         Length Name                                                                                                                                                                                                                                                                               
    ----                -------------         ------ ----                                                                                                                                                                                                                                                                               
    -a----        2/15/2015  10:09 PM            309 epguides.css                                                                                                                                                                                                                                                                       
    -a----        2/15/2015  10:09 PM           3048 modify.js                                     
    
    
  • #22780
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant
    Get-VM | Out-GridView -PassThru | Remove-VM

    This will give you a nice list view of all your VMs, and then if you select any of them and press OK, they will be removed.

  • #22788
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    Thanks.

  • #22809
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    a purely academic question. can I eschew the use of variable and still access the array?

    instead of saving [blockquote]get-childitem[/blockquote] into a variable I tried to do .

    [blockquote]get-childitem[1..3] | Remove-Item -Force[/blockquote]

    and failed.

  • #22811
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    You've got Select-Object for filtering results in the pipeline.

    Get-Process | Select-Object -Skip 2 -First 3 -Last 1 | Stop-Process

    This skips the first two results, then selects the first 3 results after skipping, and the last one, but you should keep in mind this is a very imprecise way of filtering results.

  • #23580
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    ok. I found an even shorter way to write it out. put the cmdlet in parenthesis.

    for example.

    (ls)[5..7]

You must be logged in to reply to this topic.