Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
WScript.Shell is a powerful object in Windows Script Host (WSH) that allows users to interact with the Windows operating system through scripts. It provides capabilities to run programs, manipulate the Windows environment, and automate tasks. This article will guide you through using WScript.Shell with practical examples to automate tasks on a Windows system.
You can use WScript.Shell to launch applications. The following VBScript example demonstrates how to open Notepad using WScript.Shell:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe"
To execute this script, save it with a .vbs
extension and double-click the file.
WScript.Shell can also send keystrokes to applications. This is useful for automating tasks that require user input. The following script opens Notepad and types "Hello, World!":
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "notepad.exe"
WScript.Sleep 1000
WshShell.SendKeys "Hello, World!"
You can read and modify environment variables using WScript.Shell. Here’s how to display the PATH environment variable:
Set WshShell = WScript.CreateObject("WScript.Shell")
MsgBox WshShell.ExpandEnvironmentStrings("%PATH%")
WScript.Shell can create shortcuts to files or applications. The following script creates a desktop shortcut to Notepad:
Set WshShell = WScript.CreateObject("WScript.Shell")
DesktopPath = WshShell.SpecialFolders("Desktop")
Set Shortcut = WshShell.CreateShortcut(DesktopPath & "\Notepad.lnk")
Shortcut.TargetPath = "C:\Windows\System32\notepad.exe"
Shortcut.Save
You can execute command-line instructions using WScript.Shell. Here's how to run a simple command:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo Hello, CMD!"
This script opens a command prompt, executes the echo
command, and then closes the prompt.
WScript.Shell is a versatile tool for automating tasks on Windows. By using the examples provided, you can start creating scripts to streamline your workflow and improve productivity.