PowerShell Scripts

22 March 2023 4 minutes Author: Endpool

PowerShell is… Its capabilities and the area in which it is used

PowerShell is a powerful command-line and scripting tool developed by Microsoft to manage and automate tasks on Windows systems. Using PowerShell, you can easily automate routine tasks and manage various operating system components such as file systems, registry, network, services, and more. PowerShell article covers all aspects of working with this tool, from its basic principles and concepts to more complex tasks such as deploying and managing cloud environments. You’ll learn how to create, run, and debug PowerShell scripts, and how to use powerful features like modules, objects, and variables.

Moreover, the article covers various examples of using PowerShell to manage files and folders, the registry, services, networking, and other Windows components. She also looks at using PowerShell in conjunction with other technologies such as Active Directory, Exchange, and SharePoint. Finally, the article describes the main benefits of PowerShell, such as improved performance and flexibility, and how it can be used to improve security and ensure regulatory compliance. This article is an essential resource for IT professionals who want to learn more about PowerShell and use it to automate and manage Windows systems.

How to work with PowerShell scripts?

  1. Install PowerShell on the user’s operating system (hereinafter OS). It is installed by default in Windows, there is also an option to install it on Linux and macOS;

  2. Create a file with the format name [filenme].ps1, where ps1 is the PowerShell script format;

  3. Write the code to automate the required task, save the changes in the file;

  4. Run for execution

First steps of writing PowerShell scripts
Step 1. Create a directory and a file.

1. Create a folder called powershell.

mkdir powershell

2. Move to the created folder.

cd powershell

3. Create an empty file of the appropriate type.

type nul > script.ps1

And check the existence of the file using the dir command

Step 2. Writing the code

  1. Let’s imagine that we need to create a program that will display the following data:
  • the date

  • user name

  • information about the processor

2. To write the code, open the file in a text editor, for example Sublime Text 4 and add a comment that will describe the functionality of this script and add a comment using # [comment text]

# This script gets the username, CPU information and prints the data

3.After writing the comment, we will add the following tapes to this script:

display the current date; Get-Date.

The picture shows the file after adding the commands we need

Step 3. Attempt to run the script

1. In order to test the script, we need to run it, for this we will call PowerShell from the “Start” menu by clicking on the appropriate application:

2. Let’s go to our working directory:

3. Let’s check the presence of our script using the dir command:
4. Let’s try to run the script using the following command: .script.ps1

 

Step 4. Setting the ExecutionPolicy

As you can see, we received a message that this user is unable to call scripts to execute, so let’s figure out how to fix this. For the security of the Windows OS, Microsoft engineers decided to create certain policies that would limit users in certain actions. That is why, by default, in most versions of OC Windows, an ordinary user does not have access to run scripts. The parameter responsible for this access is called ExecutionPolicy.

  1. In order to understand which value to set for this parameter – display the list of possible parameters for setting using the following command:

Get-ExecutionPolicy -List

2. The figure shows Scope (hereinafter scope), which is responsible for a certain sphere of influence of the ExecutionPolicy parameter:

  • Set by group policy for all computer users;
  • Set by group policy for the current computer user;
  • Only affects the current PowerShell session;
  • Affects current user only;
  • A default scope that affects all users of the computer.

example

3. To view the ExecutionPolicy parameter only for the CurrentUser scope, you can use the following command:

Get-ExecutionPolicy -Scope CurrentUser

Currently, we only need permissions to run scripts for the current user, so the required scope is CurrentUser.

4. To set the value of a certain policy, the Set-ExecutionPolicy command is used, let’s apply it:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Installation example.

Let’s check if everything was installed successfully.

Step 5. Retry running the script

  1. Congratulations! You have received information about the user’s computer, his username, and the current date. To improve your knowledge, I recommend going to the following links:

2. To view the ExecutionPolicy parameter only for the CurrentUser scope, you can use the following command: NishangNishang

An example of a check.
Other related articles
Found an error?
If you find an error, take a screenshot and send it to the bot.