Script for counting no. of lines in CSV file and if it exceeds more than 1 run a simple command. RRS feed

  • Question

  • Hello Scripters,

    I am new to Powerhshell world. I like to research and try before i seek another human's help. So, its obvisous that i am here after searching for a solution but couldnt find one. 

    i learned how to get the print queue details in to a CSV: 
    Get-WMIObject Win32_PrintJob | export-csv -Path C:\_Scripts_Jaifer\PrinterJobs\Report.csv -NoTypeInformation

    i learned how to count the lines. 
    Import-Csv C:\_Scripts_Jaifer\Report.csv | Measure-Object

    i learned how to get it mailed: 
    $body = Get-Content -Path C:\_Scripts\PrintJobs\log.csv -Raw
    Send-MailMessage -To myname@yahoo.com -From scom@yahoo.com -Subject 'HP Printer Queued Job Details' -Body $body -SmtpServer 'smtp.yahoo.com'

    But i fail at writing a smart script (using If statement) and mail the CSV file only when the Job Queue is higher than 1. 

    Can somebody please help me convert these seperate codes in to one smart script. I would really appreciate it. Thanks all!..

    Monday, March 9, 2020 6:52 AM

All replies

  • To count lines in a file:

    if((Get-Content file.exe).Count){
        # greater than one

    You need to stop now and learn PowerShell.  We cannot teach you this one line at a time.  The forum is only for people who have learned basic PowerShell.  That is your responsibility.

    We also will not do your homework for you.  If you need help ask your instructor for help.


    • Edited by jrv Monday, March 9, 2020 8:46 AM
    Monday, March 9, 2020 8:44 AM
  • Sorry shakthiman.
    Monday, March 9, 2020 10:03 AM