Hi, I need a powershell script that would stop a windows service ,wait for2 mins,delete a particular file from C:\documents&settings andthen start the service again.Isthis possible ?Thanks
0 Comments   [ - ] Hide Comments


Please log in to comment

Community Chosen Answer



you can create a batch file and use psexec to push it out.

net stop service

PING -n 1 -w 120000 >NUL

del c:\path\file /q /y

net start service

Answered 05/12/2014 by: SMal.tmcc
Red Belt

Please log in to comment
Answer this question or Comment on this question for clarity



Yes, it is possible.

Why dont you research it? It will only take you about 30mins from start to finish.

  1. Google how to stop and start a service in PS
  2. Look up the sleep PS equivalent
  3. Lookup how to delete a file
    If your fancy you could use Test-Path with a If statement.

    Example Pseudocode
    Test-Path (hey does this file exsit?)
       Yes, lets delete it!
       No, Oh no file, im doing nothing.
  4. Since you know how to stop the service, you'll already know how to start it...
Thats it! 
Answered 05/11/2014 by: rileyz
Red Belt

  • Hi rileyz,

    Would this be correct ? I will save and run this from the management server on remote computers.I need all in one script.

    $service = "*****"
    Import-CSV D:\FIxmachines\input.csv | % {
    $computer = $_.ComputerName

    #$result = (gwmi win32_service -computername $computer -filter "name='$service'").stopservice()
    Start-Sleep -s 120
    function delete-remotefile {
    $file = "\\$_\c$\Documents&Settings\*******.exe"
    if (test-path $file)
    echo "$_ ********.exe exists"
    Remove-Item $file -force
    echo "$_ install.exe file deleted"
  • }

    Get-Content D:\Fixmachines\machines.txt | delete-remotefile
    $result = (gwmi win32_service -computername $computer -filter "name='$service'").startservice()}
    • Right I've given this some thought and you didnt give us all the information!

      What is the target OS?
      Does it have powershell, if so what version?
      -can it be accessed remotely?
      Can the remote system take remote WMI calls?
      • XP. Don't think the workstations have powershell.They can be accessed remotely and and can take WMI calls
Please log in to comment