1 of 20

CREATING SCRIPTS IN POWERSHELL.

2 of 20

INTRODUCTION TO POWERSHELL

  • What is PowerShell ?
  • PowerShell is a powerful command-line tool and scripting language developed by Microsoft for automating system management and administration. Unlike the classic command-line tool (CMD), PowerShell is based on .NET and uses an object-oriented approach.
  • It combines three main components:
  • Interactive command shell – you can enter commands in real time.
  • Scripting language – allows you to write complex automation scenarios.
  • Runtime environment – processes commands and manages system processes.

3 of 20

WHY DO YOU NEED POWERSHELL?

  • PowerShell is used for: �✅ Automating tasks – managing users, files, processes, network settings, and updates. �✅ Managing Windows and Linux – it works on different operating systems, making it a universal tool. �✅ Integrating with various services – you can manage cloud services ( Azure , AWS), databases, and server applications. �✅ Writing powerful scripts – it supports complex structures (conditional statements, loops, functions, modules). �✅ Working with objects – PowerShell returns objects, not just text, which simplifies data processing.
  • Example of a simple command:
  • Get-Process
  • This command lists all running processes on the system.
  • Example script:
  • # Create a file with the current date
  • $ date = Get-Date -Format " yyyy -MM-dd"
  • $ filePath = "C:\Logs\log_$date.txt"
  • New-Item -Path $ filePath -ItemType File

This script creates a new file in the C:\Logs folder with the date in the name.

4 of 20

POWERSHELL

  • PowerShell is more than just a command line; it's a powerful automation and management tool with a wide range of capabilities.
  • 🔹 1. Working with commands ( Cmdlets )
  • PowerShell uses Cmdlets , which are built- in commands designed to perform various tasks.
  • Examples:
  • Get-Process # Get a list of processes
  • Get-Service # View all services
  • Get- ChildItem C:\ # View files and folders in a directory

5 of 20

POWERSHELL

  • 🔹 2. Working with variables and data
  • PowerShell allows you to create and use variables to store data.
  • Example:
  • $name = "PowerShell"
  • Write-Output " Hello, $ name!" # Outputs: Hello, PowerShell!
  • 🔹 3. Managing files and folders
  • PowerShell makes it easy to create, modify, and delete files and folders. Example:
  • New-Item -Path "C:\Test" -ItemType Directory # Create a folder
  • New-Item -Path "C:\Test\file.txt" -ItemType File # Create a file
  • Remove-Item "C:\Test\file.txt" # Delete file

6 of 20

POWERSHELL

  • 🔹 4. Process and Service Management
  • PowerShell allows you to manage running processes and system services. �Examples:
  • Get-Process | Sort-Object CPU -Descending # Sort processes by CPU usage
  • Stop-Process -Name "notepad" # Close the notepad
  • Restart-Service -Name "Spooler" # Restart the print service
  • 🔹 5. Cycles and conditions
  • PowerShell supports looping and conditional constructs. Example:
  • for ($i=1; $i -le 5; $i++) { Write-Output "Iteration $i" }

7 of 20

POWERSHELL

  • 🔹 6. Working with the network
  • PowerShell can be used to manage networks and test connections. �Examples:
  • Test-Connection google.com # Checking website availability ( ping)
  • 🔹 7. Task automation (Task Scheduler )
  • You can set up scheduled script launches using the Task Scheduler . Here's an example of an automatic script launch:
  • schtasks /create / tn " MyScript " /tr "powershell.exe -File C:\Scripts\script.ps1" / sc daily / st 09:00

8 of 20

POWERSHELL

  • 🔹 8. Security and Administration
  • PowerShell allows you to manage users, groups, and security policies. �Examples:
  • Get- LocalUser # List of local users
  • Add- LocalGroupMember -Group "Administrators" -Member "User1" # Add a user to the administrators group
  • 🔹 9. Integration with cloud services
  • PowerShell is used to manage Azure , AWS, Microsoft 365, and other cloud services. Azure login example :
  • Connect- AzAccount
  • 🔹 10. Expand capabilities through modules
  • PowerShell supports loading additional modules. �For example:
  • Install-Module -Name Az # Install the module to work with Azure
  • Import-Module Az # Add module

9 of 20

POWERSHELL

  • PowerShell can be run in several development environments, each with its own features and conveniences. The main tools for working with PowerShell are:

PowerShell console ( PowerShell Console )

Description:

  • PowerShell command line built into Windows.
  • Allows you to execute commands, run scripts and manage the system.

How to open:

  • Press Win + R , type powershell , press Enter .
  • Or search for "Windows PowerShell " in the Start menu.

Example of use:

  • Get-Process # Get a list of running processes

10 of 20

POWERSHELL

  • 🔹 2. PowerShell ISE (Integrated Scripting Environment)
  • Description:
  • PowerShell ISE is a built-in Windows environment for working with PowerShell scripts.
  • Allows you to write, test, and debug scripts in a convenient graphical interface.
  • How to open:
  • Windows PowerShell ISE into the search box. and run.
  • Or run the command in the PowerShell console:

ise

Example of use:

Write-Output "Hello, PowerShell ISE!"

11 of 20

POWERSHELL

🔹 3. Visual Studio Code (VS Code) + PowerShell Extension

Description:

  • VS Code is a modern and powerful code editor from Microsoft.
  • With the PowerShell extension ( PowerShell Extension ) it becomes a full-fledged environment for writing and debugging PowerShell scripts.

How to install:

  1. Download and install VS Code from the official website .
  2. Open VS Code, go to Extensions ( Ctrl + Shift + X ).
  3. Find and install the PowerShell extension .
  4. You can now write PowerShell scripts directly in VS Code.

Example of use:

# Example script in VS Code

$date = Get-Date

Write-Output " Today's date: $ date"

12 of 20

🔹 CONCLUSION: WHICH ENVIRONMENT SHOULD YOU CHOOSE?

Environment

Who is it suitable for?

Peculiarities

PowerShell Console

Fast execution of commands

Suitable for one-off commands, no code editor

PowerShell ISE

For beginners in script writing

Convenient debugging, but outdated interface

VS Code + PowerShell Extension

For developers and administrators

Best choice: powerful editor, extension and debugging support

Recommendation

✅ For simple commands , use PowerShell Console . �✅ For beginners , PowerShell ISE is a good choice . �✅ For development and professional administration , VS Code with the PowerShell extension is the best choice!

13 of 20

AUTOMATING TASKS WITH POWERSHELL

  • PowerShell is a powerful tool for automating routine tasks such as managing files, services, users, backups, and task scheduling.
  • 🔹 1. Running scripts (.ps1)
  • You can create and run a PowerShell script to automate actions.
  • 📌 Example: Creating and running a script
  • Open Notepad and enter the code:
  • Write-Output " Script executed!"

2. Save the file as script.ps1.

3. Run it in PowerShell :

.\script.ps1

14 of 20

AUTOMATING TASKS WITH POWERSHELL

  • 🔹 2. Task Scheduler
  • up automatic script launching via Task Scheduler .
  • 📌 Example: Creating a task to run a script daily
  • $Action = New- ScheduledTaskAction -Execute "powershell.exe" -Argument "C:\Scripts\backup.ps1"
  • $Trigger = New- ScheduledTaskTrigger -Daily -At 9AM
  • Register- ScheduledTask - TaskName " BackupTask " -Action $Action -Trigger $Trigger -User "SYSTEM"
  • This code creates a task that runs backup.ps1 every day at 09:00.

15 of 20

AUTOMATING TASKS WITH POWERSHELL

  • 🔹 3. Automate file management
  • You can automatically create, modify, or delete files.
  • 📌 Example: Copying files and creating a backup
  • Copy-Item "C:\Data\file.txt" -Destination "C:\Backup\file_$(Get-Date -Format yyyyMMdd ).txt"
  • This script creates a backup file with the date in the name.
  • 🔹 4. Automatic process control
  • You can monitor processes and automatically close unnecessary ones.
  • 📌 Example: Close Notepad if it is open
  • $process = Get-Process -Name "notepad" - ErrorAction SilentlyContinue
  • if ($process) { Stop-Process -Name "notepad" -Force }

16 of 20

AUTOMATING TASKS WITH POWERSHELL

  • 🔹 5. System monitoring and notifications
  • You can automatically check the system status and send reports.
  • 📌 Example: Checking free disk space and notification
  • $disk = Get- PSDrive -Name C
  • if ($ disk.Free - lt 10GB) {
  • Send- MailMessage -To "admin@example.com" -From "server@example.com" -Subject " Low disk space" - Body " Less than 10 GB left on drive C !" - SmtpServer "smtp.example.com"
  • }
  • 🔹 6. Automate user management
  • You can add or remove users automatically.
  • 📌 Example: Creating a new user
  • New- LocalUser -Name "User1" -Password ( ConvertTo-SecureString "Pass123" - AsPlainText -Force) - FullName "User One" -Description " New user"

17 of 20

PRACTICE: WRITING A SIMPLE POWERSHELL SCRIPT

Task:

Write a PowerShell script that:

  1. Creates the C:\Logs folder if it does not exist.
  2. Creates a text file in this folder with the current date in the name.
  3. Writes the current date and time to a file.
  4. Displays a success message.

18 of 20

PRACTICE: WRITING A SIMPLE POWERSHELL SCRIPT

🔹 Solution:

log_creator.ps1

# 1. Determine the path to the folder

$ folderPath = "C:\Logs"

# 2. Check if the folder exists, if not, create it.

if (!(Test- Path $ folderPath )) {

New- Item - Path $ folderPath - ItemType Directory

Write-Output "The folder $ folderPath has been created."

}

# 3. Form a file name with the current date

$ date = Get- Date -Format " yyyy -MM- dd "

$ filePath = "$ folderPath \log_$date.txt"

# 4. Write the current date and time to a file

$ logMessage = "Execution date and time: $(Get- Date )"

$ logMessage | Out-File - FilePath $ filePath - Append

# 5. Display a message about successful completion

Write-Output "File $ filePath successfully created and updated!"

19 of 20

PRACTICE: WRITING A SIMPLE POWERSHELL SCRIPT

🔹 How to run this script:

  1. Create a file log_creator.ps1 in Notepad or VS Code.
  2. Save the file to a convenient location, such as C:\Scripts\log_creator.ps1 .
  3. Launch PowerShell and run the command:

C:\Scripts\log_creator.ps1

4. Checking the result: The file log_YYYY-MM-DD.txt will appear in the C:\Logs folder, containing a record of the date and time of execution.

🔹 Extra Assignment (for advanced learning) Modify the script to delete files older than 7 days. Hint: Use the command:

Get- ChildItem "C:\Logs" | Where-Object { $_. LastWriteTime - lt (Get-Date). AddDays (-7) } | Remove-Item

20 of 20

THANK YOU FOR YOUR ATTENTION