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

#3 PowerShell Learning: Writing and Running PowerShell Scripts

August 14, 2023
in Blog, PowerShell Learning
0
ADVERTISEMENT

Table of Contents

Microsoft Windows PowerShell has the ability to perform so many actions in an interactive fashion. For many network administrators, one-line PowerShell commands will indeed solve many routine problems. This can become extremely powerful when the commands are combined into files known as scripts. However, there are some very good reasons to write Windows PowerShell scripts.

 the number-one reason to write a Windows PowerShell script is to address recurring needs. As an example, consider the activity of displaying a directory listing. The simple Get-ChildItem cmdlet does a good job, but after you decide to sort the listing and filter out only files of a certain size, you end up with the command shown here

PS C:\> Get-Service | Where-Object {$_.Status -eq 'Running'} | Sort-Object -Property Name

Status   Name               DisplayName
------   ----               -----------
Running  AdobeUpdateService AdobeUpdateService
Running  Appinfo            Application Information
Running  AudioEndpointBu... Windows Audio Endpoint Builder
Running  Audiosrv           Windows Audio
Running  BDESVC             BitLocker Drive Encryption Service
Running  BFE                Base Filtering Engine

Even using tab completion, the above command requires a lot of typing. One way to shorten it would be to use aliases and create a user-defined function. The easiest solution is to write a PowerShell script.

To writing scripts, I would recommend using Windows PowerShell ISE or Visual Studio Code. For more details on Windows PowerShell ISE, visit this page. Change the PowerShell ExecutionPolicy on your computer to allow script execution as shown here.

To write your first script, Open PowerShell ISE and copy the following lines and paste into Script Pane.

#Get running services
Get-Service | Where-Object {$_.Status -eq 'Running'} | Sort-Object -Property Name

#Counting running services 
(Get-Service | Where-Object {$_.Status -eq 'Running'}).Count

#Counting Stopped services 
(Get-Service | Where-Object {$_.Status -eq 'Stopped'}).Count
Hx9LTSPn5b0kydLH7BIsLfLWTw6q2ywiJ3qgxowIC5zZrBXn6m5WMfqrh2ad

After writing the above code, click on save button and save the file with any name, but the file extension should be ps1.

Running PowerShell Scripts

There are several ways to run the PowerShell scripts but the first step is to set the PowerShell ExecutionPolicy. The default ExecutionPolicy is Restricted however the recommended setting to allow execution of PowerShell scripts is RemoteSigned. Learn more about the ExecutionPolicy here.

Use the following command to change the ExecutionPolicy to RemoteSigned:

Set-ExecutionPolicy RemoteSigned -Force

To run the PowerShell script, navigate to the directory containing script file and then run the script:

PS C:\> Set-Location D:\scripts\
PS D:\scripts> .\Get-Services.ps1

Where .\ tells the PowerShell console to locate and run Close-Outlook.ps1 script file in current working directory.

If you didn’t like the above approach for running the script, you can directly run the script entering its path like:

D:\scripts\Get-Services.ps1

If the file name or path contains any space, make sure to enclose the complete path in double quotes like:

PS D:\> & "D:\scripts\Get Services.ps1"

Status   Name               DisplayName
------   ----               -----------
Running  AdobeUpdateService AdobeUpdateService
Running  Appinfo            Application Information
Running  AudioEndpointBu... Windows Audio Endpoint Builder

Executing Script from Run Dialog

If you want to run a PowerShell script from Run dialog or cmd.exe, you cannot do so by directly typing its path as we did above. If you do so, the script will be opened in text editor. To make it run, you need to call the powershell.exe and then pass the PowerShell script as an argument with -File parameter.

To make it more clear, lets run the same script again. In Run dialog, type following and hit enter:

powershell.exe -noexit -file "D:\scripts\Get-Services.ps1"
  • The -noexit parameter keeps the PowerShell console open even after script is executed. If you don’t want to keep the console open after script execution, don’t use -noexit parameter.
  • The -file parameter is used to specify the complete path of script.
rCLhBe3cKk3RnC3Mps8Msd8mIIx2Sj4S6JPNXV6sybo1EAf0juSRdsx7Z3xj

You can optionally use -windowstyle parameter to specify how PowerShell console will look. It accepts values such as Hidden, Minimized, Maximized etc. which are self explanatory.

Schedule PowerShell Script with Task Scheduler

To schedule a PowerShell script in Windows Task Scheduler, you can use the approach mentioned for executing the PowerShell script via Run dialog. Under Actions tab, in Action section:

  • Select Start a program.
  • In program/script box type powershell.exe.
  • In Add arguments box, type -file along with path of script. See the following image for details.
TcGGz6gKQvaP4aEFXlFcbBTSJZA6hSLqHUTJoPOKd7SoUEv3D2sBIbuPR0xw
5/5 - (1 vote)
Previous Post

PowerShell Learning: Getting Started with Windows PowerShell

Next Post

#2 PowerShell Learning: Working with Windows PowerShell

Related Posts

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
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 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