Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How to Create a PowerShell Script?

July 11, 2024
in A, Blog, Powershell
0
ADVERTISEMENT

Table of Contents

System administrators appreciate a useful shortcut, particularly for reducing the frustration associated with managing computers, addressing user problems, and handling emergencies in the office. This is where PowerShell comes into play.

While dozens of excellent scripting languages are available, PowerShell stands out among them. Its cross-platform capabilities make it an invaluable tool regardless of the operating environment. System administrators, burdened with numerous tasks, are finding automation increasingly essential. Starting with the basics of PowerShell is an excellent first step in this direction.

Create a new file and add a cmdlet

1. Open the Notepad or any your favorite text editor in your computer.

2. Write a new or paste your script in the text file. For example:

5NE6t2bMRZd8YN7ZWcD1ofmIIDxpJA3HYYkK9cuDx62oqPLC0pXMMxSv3L3C

3. Click the File menu then select the Save As option

VXgw1NphqySCYGHIsZeiz6qvBZYggMdSCVstjvn6fez7sqjh84bLBCkXUj2U

4. Save the script in the .ps1 file format: 

  • Select the location to save the script. For example, we’ll save it in D:\scripts folder.
  • File name: Confirm a descriptive name for the script, for example, ping.ps1.
  • Save as type: Select All Files to save the file with a custom extension instead of the text file format.
uYJa1Pnz0i3mj6T0MSIaQZ2zdi5g9U6UgsnZJ4JgHbCDW1Cte8qKYvBTO392

5. Click the Save button.

Save and try to run the script

To run the script, the most common method is to call it in the PowerShell terminal. (You can also use the PowerShell ISE or VS CodeVS Code.)

& "D:\Scripts\ping.ps1"

Go ahead and try that command. You should get an error that says scripts are disabled on your system. This is for security reasons.

KWeOthnBkH45flVaZDqwAiyKXDUwBpzCLepkME7OiPBFJTciXi09btiZL1Ep

Modify the execution policy

To safeguard your system against malicious scripts, PowerShell implements an execution policy. To execute our new script, we must adjust this policy to permit the running of our example PowerShell script. There are four distinct execution policies:

  • Restricted: Scripts won’t run. Period. (Default setting in Windows 10/11).
  • RemoteSigned:  Locally created scripts run. Scripts that were created on another machine won’t run unless they are signed by a trusted publisher. (Default setting in Windows server).
  • AllSigned: Scripts (including locally created scripts) only run if signed by a trusted publisher.
  • Unrestricted: All scripts run regardless of who created them and whether they’re signed.

To get the execution policy applied to the computer. Let’s run the following command. To get a list of available execution policies, run the command with the -List parameter.

Get-ExecutionPolicy
QwKVxmFXdNUOGOSeSmJotrfKbsl0IROcw59j4xAW5SGDTT6jeTtDAdfeqibk

Since we have not digitally signed our new PowerShell example script, our options for the execution policy are limited to RemoteSigned and Unrestricted. We are recommended to change it to RemoteSigned.

To change the execution policy, reopen PowerShell as an administrator (the command fails otherwise), and run the following command:

 Set-ExecutionPolicy RemoteSigned

The Set-ExecutionPolicy cmdlet asks to verify that you really want to change the execution policy. Go ahead and select Y for yes, then close and reopen your PowerShell window.

BuLq1ApFbM2sIIslCxBmqMQdtJsw9pfabULPcAijzyAZQlfDnG0HaOjfNO3E

Run your script

After restarting the PowerShell window, try running your .PS1 script again. It should print the results into the window.

pPauamtVDgpBYeJAywCJPzIVFE4FKl2GlpUyy9fhEl2bgtXVxCNeuuuGcfoH

Congratulations, you just wrote your first PowerShell script!

PowerShell examples for beginners

When looking for ideas on what you can do with beginner scripts, just think about things you need to do manually and see if can grab the information.

Print the ‘Hello World’ string to the output with a custom color:

Write-Host "Hello World!" -ForegroundColor Green
d9GdKhpbDwoKA5Fa2aa9EE74OZsZtxdf5sntv8xkXCiADwpRwoSHixJpJM9P

Get a running process on the window system:

Get-Process -Name 'msedge'
aRdene2N3k84l4avlZ0aX0DOqnmeDDipt0Up1A49grODxkQ03BwL1zJSsu6y

Or try clearing out a temp folder that is taking up space with unneeded documents:

Remove-Item -Path C:\Windows\Temp\ -Recurse -Force

These are immediate advantages that you can utilize straight away. The possibilities become endless once you delve into modules and begin scripting for critical systems such as Azure, VMWare, AWS, or Active Directory.

ADVERTISEMENT

Not a reader? Watch this related video tutorial:

5/5 - (1 vote)
Previous Post

Export Microsoft 365 Disabled Users Report Using Microsoft Graph

Next Post

How to Enable Outlook Modern Authentication from Registry Setting

Related Posts

Images Hidden Due To Mature Content Settings In CivitAI

August 31, 2024

Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

August 20, 2024

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024

How to Remove The Test Mode Watermark Without Disabling Test Mode

July 28, 2024

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory