Operating System

Step-by-Step Guide: Enabling a PowerShell Script in Windows 7

Microsoft task automation framework is Windows PowerShell which considers a command line shell with a scripting language on .NET Framework. In PowerShell, cmdlets (command-lets) perform administrative tasks. These cmdlets specialize in .NET classes that do a specific operation. In Windows 7, the execution of the PowerShell script is disabled by default. Therefore, you may ask yourself how do I enable a PowerShell Script in Windows 7. In this article, we are going to tell you the ways to enable, create and change a PowerShell Script.

How to Allow Execution of PowerShell Scripts on Windows 7

To do so, follow the steps below:

  1. Go to the Start Menu.
  2. Look for Accessories.

Picture: https://www.techentice.com/allow-execution-of-powershell-scripts/

  • Find Windows PowerShell and select it.

Picture: https://www.techentice.com/allow-execution-of-powershell-scripts/

  • Right-click on the shortcut powershell.
  • Select Run as Administrator.
  • Hit Yes.

Picture: https://www.techentice.com/allow-execution-of-powershell-scripts/

  • Use the Set-ExecutionPolicy cmdlet to change the script execution policy.
  • Input Set-ExecutionPolicy unrestricted.
  • Hit Enter.
  • You will be asked whether you would like to change the script execution policy or not. Press Y.
  • Windows PowerShell Script execution is now enabled.

How to Create PowerShell Script

It is time to know how to create PowerShell Script. To do so, follow the steps below:

  1. Go the Windows PowerShell ISE.
  2. Hit in the top pane (labeled Untitled1.ps1), and type the following text.

Get-EventLog –List

  • Hit Enter.

Note: The command does not execute after this, but it starts a line 2 instead.

  • To run the script, click the green button, or press F5. In this part, you can see the result displayed in the middle pane. In the case that it finds an error, a red text will appear. Find these characters : <<<< if you see an error which refers to a specific string of text that caused this problem.
  • Hit Save on the menu or you can press Ctrl + S to save the icon.
  • Take a look at folder named C:\ Scripts and change Untitled to LogInfo to save the script as C:\Scripts\LogInfo.ps1.
  • Change the place of the cursor to the bottom of the screen.
  • Type C:\Scripts\LogInfo.ps1.
  • Hit Enter to execute the script.
  • Use the command below to change the current location to the script file:

Set-Location C:\Scripts.

  1. Run the script with the command below (it will fail):

LogInfo.ps1

  1. Hit the up arrow and change the last command

.\LogInfo.ps1

How to Change a Scrip Using a ForEach Loop

  1. Select File → New to make a blank area to work with a new script.
  2. Hit the following text or press F5 to execute it.

get-service | select-object name, status

This list provides all the services on the system by name and their current status.

  • The following command will store the result in a collection with the name of $colService.

Note: Notice that you don’t have to retype the entire command, but instead you just add $colService = to the beginning of the line:

$colService = Get-service | select-object

name, status

  • To loop through the collection, add a ForEach loop. The task of loop is to check each service stored in the collection and then use the Write-Host cmdlet to display the only services with a status equal to running.

ForEach ($item in $colService) {

If ($item.status -eq “running”)

{write-host $item.name}

}

  • You can use the same method to list all the services that are stopped. Therefore you need to change running to stop in the script.
  • There is more than one way to get what you need. The following one-liner can be used to get a list of running services as an example:

Get-Service | Select name, status | where {$_.status -eq “running”}

Conclusion

There are some easy steps to enable, create and change a PowerShell Script. In this article, we presented these all fully to help you use PowerShell properly. Do you know any other way to create a PowerShell script? Share your ideas with us.

Reyhane A

Reyhane is an author and technology enthusiast who has a passion for writing about Windows tips and tricks. Born and raised in Tehran, Iran, Reyhane always had an interest in technology, which led her to pursue a degree in computer engineering from the University of Tehran. After completing her studies, Reyhane began working as a software engineer, where she gained a wealth of experience and knowledge in the field of technology. She soon discovered her love for writing, and began sharing her knowledge with others through her blog. Reyhane's blog quickly gained popularity, and she became known as an expert in the field of Windows tips and tricks. Her articles are known for being clear, concise, and easy to understand, making them accessible to everyone, regardless of their level of technical expertise. Today, Reyhane continues to share her knowledge with the world through her blog and YouTube channel, where she provides tips and tricks for Windows users to help them optimize their computer's performance and get the most out of their technology. In addition to her passion for technology, Reyhane enjoys traveling, photography, and spending time with her family and friends. She is also an avid reader and is always on the lookout for new books to add to her collection. Reyhane's dedication to her work and her love for technology have made her a respected figure in the industry, and she continues to inspire and educate others through her writing and online content.

Leave a Reply

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

Back to top button