Search
Close this search box.

Starting and stopping Application Pool using Powershell

Hi,

Providing sample powershell script to start and stop Application Pool of an application hosted in IIS.

Save the below script in a file called as IISCmds.ps1
#Start To Start Application Pool

    Function fnStartApplicationPool([string]$appPoolName)
    {
	  import-module WebAdministration
       if((Get-WebAppPoolState $appPoolName).Value -ne 'Started')
       {
	      Start-WebAppPool -Name $appPoolName
       }
    }
    
#End To Start Application Pool

#Start To Stop Application Pool

    Function fnStopApplicationPool([string]$appPoolName)
    {
	  import-module WebAdministration
       if((Get-WebAppPoolState $appPoolName).Value -ne 'Stopped')
       {
	      Stop-WebAppPool -Name $appPoolName
       }
    }
    
#End To Stop Application Pool

*	Caller of IISCmds.ps1 file.
    
	#Start - To Stop Application Pool

	$FTPUser = '$$$$$' #Provide Server User Name
        $FTPPassword = '*******' #Provide Password
        $UserName=$FTPUser
        $Password=$FTPPassword
        $ServerName = 'Server IP Address'
	$appPoolName = 'TestSites'  #Application Pool Name

	$pwd = convertto-securestring $Password -asplaintext -force
        $cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $UserName,$pwd

        invoke-command -computername $ServerName -Credential $cred `
        -ScriptBlock ${function:fnStopApplicationPool} `
        -ArgumentList $appPoolName

	#End - To Stop Application Pool

	#Start - To Start Application Pool

	$FTPUser = '$$$$$' #Provide Server User Name
        $FTPPassword = '*******' #Provide Password
        $UserName=$FTPUser
        $Password=$FTPPassword
        $ServerName = 'Server IP Address'
	$appPoolName = 'TestSites'  #Application Pool Name

		$pwd = convertto-securestring $Password -asplaintext -force
        $cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $UserName,$pwd

        invoke-command -computername $ServerName -Credential $cred `
        -ScriptBlock ${function:fnStartApplicationPool} `
        -ArgumentList $appPoolName

	#End - To Start Application Pool

Thanks!!!

This article is part of the GWB Archives. Original Author: Developers Friendly

Related Posts