0

I have a PowerShell command which removes virtual Folder from the IIS (8.5)

Remove-Item IIS:\Sites\WebsiteName\VirtualFolderName -Force -Recurse

This works just fine when executed standalone from PowerShell console, but I want to run this from inside of a VBScript. I have VBScript something like this:

Set objShell = CreateObject("WScript.Shell")
objShell.Run("powershell.exe -noexit -Command='Remove-Item IIS:\Sites\WebsiteName\VirtualFolderName -Force -Recurse'")

When I execute above it wont work, and it just spits out the command on the PowerShell console.

Any suggestions here?

1 Answer 1

2

Single quotes only work inside PowerShell. You need double quotes around the PowerShell statement, and you must double them to escape them inside the VBScript string. Also, remove the = between the parameter -Command and its argument. If the module WebAdministration isn't loaded automatically you need to do it yourself, otherwise you won't have a drive IIS:.

Change this:

objShell.Run("powershell.exe -noexit -Command='Remove-Item IIS:\Sites\WebsiteName\VirtualFolderName -Force -Recurse'")

into this:

objShell.Run("powershell.exe -noexit -Command ""Import-Module WebAdministration; Remove-Item IIS:\Sites\WebsiteName\VirtualFolderName -Force -Recurse""")
Sign up to request clarification or add additional context in comments.

2 Comments

got error : Remove-Item : Cannot find drive. A drive with the name 'IIS' does not exist.
@Pankaj Load the module WebAdministration. See updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.