Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use WScript.Shell to Automate Tasks in Windows

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.

Examples:

Example 1: Running a Program

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.

Example 2: Sending Keystrokes

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!"

Example 3: Modifying Environment Variables

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%")

Example 4: Creating a Shortcut

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

Example 5: Executing Commands via CMD

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.

Conclusion

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.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.