CREATING SCRIPTS IN POWERSHELL.
INTRODUCTION TO POWERSHELL
WHY DO YOU NEED POWERSHELL?
This script creates a new file in the C:\Logs folder with the date in the name.
POWERSHELL
POWERSHELL
POWERSHELL
POWERSHELL
POWERSHELL
POWERSHELL
PowerShell console ( PowerShell Console )
Description:
How to open:
Example of use:
POWERSHELL
ise
Example of use:
Write-Output "Hello, PowerShell ISE!"
POWERSHELL
🔹 3. Visual Studio Code (VS Code) + PowerShell Extension
Description:
How to install:
Example of use:
# Example script in VS Code
$date = Get-Date
Write-Output " Today's date: $ date"
🔹 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!
AUTOMATING TASKS WITH POWERSHELL
2. Save the file as script.ps1.
3. Run it in PowerShell :
.\script.ps1
AUTOMATING TASKS WITH POWERSHELL
AUTOMATING TASKS WITH POWERSHELL
AUTOMATING TASKS WITH POWERSHELL
PRACTICE: WRITING A SIMPLE POWERSHELL SCRIPT
Task:
Write a PowerShell script that:
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!"
PRACTICE: WRITING A SIMPLE POWERSHELL SCRIPT
🔹 How to run this script:
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
THANK YOU FOR YOUR ATTENTION