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:
- Go to the Start Menu.
- 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:
- Go the Windows PowerShell ISE.
- 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.
- Run the script with the command below (it will fail):
LogInfo.ps1
- Hit the up arrow and change the last command
.\LogInfo.ps1
How to Change a Scrip Using a ForEach Loop
- Select File → New to make a blank area to work with a new script.
- 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.