Powershell do while loop that runs unitl I do a CTRL C
All,

I am confused on how to exactly run a while true loop.  Essentially I would like to run this command repeatedly (every second) until I cancel it out.

EX. - > get-WmiObject -class Win32_printer | select name" 

Something like this below

while true ; do (get-WmiObject -class Win32_printer | select name) |start-sleep 5;done  **** I know syntax and layout of this is wrong....I am thoroughly confused.

Thanks for the help.
May 20th, 2015 1:37pm

while ($true) {
    write-host 'do stuff...'
    Start-Sleep -Seconds 1  
} 
change the stuff in the loop to match your needs.
Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 1:50pm

kdcooke,

Thanks for your help.

Something like this works just fine....very rapid output.

PS C:\> while ($true) {Get-WmiObject -Class Win32_printer | select Name} 


Name
----
\\PRINTSRVTP01\HP_3505
\\PRINTSRVTP01\HP_3505
\\PRINTSRVTP01\HP_3505
\\PRINTSRVTP01\HP_3505

However, if I want to add the start-sleep part I can't get that to work.
For the record I am trying to run this in a one liner...not in a ps1 file if that matters.





May 20th, 2015 2:05pm

place a semicolon between commands to keep it on one line. 

while ($true) {Get-WmiObject -Class Win32_printer | select Name; start-sleep -seconds 1} 

Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 2:26pm

kdcooke,

Thank you sir.  I appreciate your help.

Rob

May 20th, 2015 2:37pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics