locked
Get-Content Where-Object RRS feed

  • Question

  • Hi,

    I am running the Get-Content cmdlet to read a text file and search for the contents. If the content is not found, I need to execute an action. I am not sure how to do the latter part.

    Get-Content -Path C:\INV.txt | Where-Object {$_ -NotLike '*no rows selected*'}

    If I don't find this text "no rows selected" I need to execute an command. Any help will be greatly appreciated.

    Thanks

    Friday, August 5, 2016 6:40 PM

Answers

  • If (!(Get-Content -Path C:\INV.txt | Select-String -Pattern 'no rows selected')) { 
                                            Write-Host 'String Not Found Execute Code' }

    Friday, August 5, 2016 6:50 PM
  • If (Get-Content -Path C:\INV.txt | Select-String -Pattern 'no rows selected') { Write-Host 'String Matched' 
        } Else { Write-Host 'Execute Code' }
    

    Friday, August 5, 2016 6:52 PM

All replies

  • If (!(Get-Content -Path C:\INV.txt | Select-String -Pattern 'no rows selected')) { 
                                            Write-Host 'String Not Found Execute Code' }

    Friday, August 5, 2016 6:50 PM
  • If (Get-Content -Path C:\INV.txt | Select-String -Pattern 'no rows selected') { Write-Host 'String Matched' 
        } Else { Write-Host 'Execute Code' }
    

    Friday, August 5, 2016 6:52 PM
  • Perfect! Thank you Vincent.
    Friday, August 5, 2016 8:01 PM