Author Posts

February 19, 2015 at 9:29 pm

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.

February 19, 2015 at 10:00 pm

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                                     

February 20, 2015 at 12:00 am

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.

February 20, 2015 at 7:51 pm

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.

February 20, 2015 at 9:57 pm

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.

March 22, 2015 at 7:33 pm

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

for example.

(ls)[5..7]