none
Powershell, ignore/skip value from if statement RRS feed

  • Question

  • Hi, I am collecting some values into an array, and using some if statements to get my values correct. One thing I need to do is to skip a value (I don't want it included in my array).

    Example:

    a,b,c,d,e,f

    I only want to collect a,b,c,d,f. Not e.

    Thanks.


    • Edited by niklasrene Thursday, November 23, 2017 11:56 AM
    Thursday, November 23, 2017 11:51 AM

Answers

  • Ah, I was just making a better example:
    $file = 'c:\tmp\blah.txt'
    
    New-Item $file -force
    Add-Content $file 'a','b','c','d','e','f'
    
    $values = Get-Content $file | where {$_ -ne 'e'}

    Rather than checking for the value and skipping it, you can just leave it out during the collection process.
    Thursday, November 23, 2017 12:05 PM

All replies

  • Edit, I think I fixed this my self.

    if ($value -like "e" {continue}

    Thursday, November 23, 2017 11:56 AM
  •  
    $Processes = Get-Process | where {$_.ProcessName -ne 'svchost'}
    Will collect all processes except the one called svchost. Is that what you want to do ?
    Thursday, November 23, 2017 11:57 AM
  • Ah, I was just making a better example:
    $file = 'c:\tmp\blah.txt'
    
    New-Item $file -force
    Add-Content $file 'a','b','c','d','e','f'
    
    $values = Get-Content $file | where {$_ -ne 'e'}

    Rather than checking for the value and skipping it, you can just leave it out during the collection process.
    Thursday, November 23, 2017 12:05 PM