Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
PowerShell is a powerful scripting language and command-line shell designed for task automation and configuration management, built on the .NET framework. It is a vital tool for systems administrators and engineers working in a Windows environment. In this article, we'll explore how to create and execute PowerShell scripts, providing practical examples to help you get started.
Creating a PowerShell Script
Open a Text Editor:
Write Your Script:
Begin by writing your PowerShell commands in the text editor. Here's a simple example that retrieves a list of running processes:
# Get-ProcessExample.ps1
Get-Process | Select-Object Name, CPU, ID
Save the Script:
.ps1
extension, which is the standard extension for PowerShell scripts. For example, save the above script as Get-ProcessExample.ps1
.Executing a PowerShell Script
Open PowerShell:
Set Execution Policy:
PowerShell's execution policy determines whether scripts can run on your system. To allow script execution, you may need to change the execution policy. Run PowerShell as an administrator and execute the following command:
Set-ExecutionPolicy RemoteSigned
This command allows scripts created locally to run, while downloaded scripts must be signed by a trusted publisher.
Navigate to Script Location:
Use the cd
command to navigate to the directory where your script is saved. For example:
cd C:\Scripts
Execute the Script:
Run the script by typing .\
followed by the script name. For example:
.\Get-ProcessExample.ps1
This command executes the script and displays the list of running processes with their names, CPU usage, and IDs.
Examples:
Example 1: Listing Files in a Directory
# ListFiles.ps1
Get-ChildItem -Path C:\Users\YourName\Documents
Execute with:
.\ListFiles.ps1
Example 2: Displaying System Information
# SystemInfo.ps1
Get-ComputerInfo | Select-Object CsName, WindowsVersion, WindowsBuildLabEx
Execute with:
.\SystemInfo.ps1