From fbcf7539659ec0f236bda092b80f912a95109bd9 Mon Sep 17 00:00:00 2001 From: serialexperiments0815 <112315380+serialexperiments0815@users.noreply.github.com> Date: Sat, 18 Oct 2025 16:45:58 +0200 Subject: [PATCH] Expanded and structurized scripts. --- .../count-files-of-specific-type.ps1 | 0 .../display-system-info.ps1 | 0 .../list-files-in-folder.ps1 | 0 .../rename-files-in-folder-by-pattern.ps1 | 0 .../to-do-text-file-through-script.ps1 | 5 +++ .../deleteOlderThan30Days.ps1 | 14 +++++++ .../deleteOlderThan30Days/testing.txt | 0 .../exportFolderFileListToCSV.ps1 | 5 +++ .../fictionalCompany/Accounting/personell.csv | 0 .../fictionalCompany/Accounting/salaries.csv | 0 .../Administration/personell.csv | 0 .../Administration/salaries.csv | 0 .../fictionalCompany/Managers/personell.csv | 0 .../fictionalCompany/Managers/salaries.csv | 0 .../Managers/upcomingPresentation.pptx | 0 .../fileFolderSorting/empty copy.png | 0 .../fileFolderSorting/empty.png | 0 .../fileFolderSorting/fileFolderSorting.ps1 | 25 ++++++++++++ .../fileFolderSorting/pdf1 copy.pdf | 0 .../fileFolderSorting/pdf1.pdf | 0 .../fileFolderSorting/test copy.mp4 | 0 .../fileFolderSorting/test.mp4 | 0 .../findAndReplaceTextMultipleFiles.ps1 | 8 ++++ .../sample.txt | 2 + .../findAndReplaceTextMultipleFiles/text1.txt | 2 + .../folderBackup/folderBackup.ps1 | 7 ++++ .../unsafe/important/important.txt | 1 + ...ckRunningProcessAndAlertIfIsNotRunning.ps1 | 14 +++++++ .../downloadFilesFromURL.ps1 | 26 +++++++++++++ .../generateReportOfInstalledPrograms.ps1 | 10 +++++ .../happenings/aFileCreated.txt | 0 .../happenings/aFileDeleted.txt | 0 .../monitorFolderAndLogChanges.ps1 | 39 +++++++++++++++++++ 33 files changed, 158 insertions(+) rename 1 - Basics/{ => count-files-of-specific-type}/count-files-of-specific-type.ps1 (100%) rename 1 - Basics/{ => display-system-info}/display-system-info.ps1 (100%) rename 1 - Basics/{ => list-files-in-folder}/list-files-in-folder.ps1 (100%) rename 1 - Basics/{ => rename-files-in-folder-by-pattern}/rename-files-in-folder-by-pattern.ps1 (100%) create mode 100644 1 - Basics/to-do-text-file-through-script/to-do-text-file-through-script.ps1 create mode 100644 2 - Simple Automation/deleteOlderThan30Days/deleteOlderThan30Days.ps1 rename 1 - Basics/to-do-text-file-through-script.ps1 => 2 - Simple Automation/deleteOlderThan30Days/testing.txt (100%) create mode 100644 2 - Simple Automation/exportFolderFileListToCSV/exportFolderFileListToCSV.ps1 create mode 100644 2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Accounting/personell.csv create mode 100644 2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Accounting/salaries.csv create mode 100644 2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Administration/personell.csv create mode 100644 2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Administration/salaries.csv create mode 100644 2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Managers/personell.csv create mode 100644 2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Managers/salaries.csv create mode 100644 2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Managers/upcomingPresentation.pptx create mode 100644 2 - Simple Automation/fileFolderSorting/empty copy.png create mode 100644 2 - Simple Automation/fileFolderSorting/empty.png create mode 100644 2 - Simple Automation/fileFolderSorting/fileFolderSorting.ps1 create mode 100644 2 - Simple Automation/fileFolderSorting/pdf1 copy.pdf create mode 100644 2 - Simple Automation/fileFolderSorting/pdf1.pdf create mode 100644 2 - Simple Automation/fileFolderSorting/test copy.mp4 create mode 100644 2 - Simple Automation/fileFolderSorting/test.mp4 create mode 100644 2 - Simple Automation/findAndReplaceTextMultipleFiles/findAndReplaceTextMultipleFiles.ps1 create mode 100644 2 - Simple Automation/findAndReplaceTextMultipleFiles/sample.txt create mode 100644 2 - Simple Automation/findAndReplaceTextMultipleFiles/text1.txt create mode 100644 2 - Simple Automation/folderBackup/folderBackup.ps1 create mode 100644 2 - Simple Automation/folderBackup/unsafe/important/important.txt create mode 100644 3 - Intermediate/checkRunningProcessAndAlertIfIsNotRunning/checkRunningProcessAndAlertIfIsNotRunning.ps1 create mode 100644 3 - Intermediate/downloadFilesFromURL/downloadFilesFromURL.ps1 create mode 100644 3 - Intermediate/generateReportOfInstalledPrograms/generateReportOfInstalledPrograms.ps1 create mode 100644 3 - Intermediate/monitorFolderAndLogChanges/happenings/aFileCreated.txt create mode 100644 3 - Intermediate/monitorFolderAndLogChanges/happenings/aFileDeleted.txt create mode 100644 3 - Intermediate/monitorFolderAndLogChanges/monitorFolderAndLogChanges.ps1 diff --git a/1 - Basics/count-files-of-specific-type.ps1 b/1 - Basics/count-files-of-specific-type/count-files-of-specific-type.ps1 similarity index 100% rename from 1 - Basics/count-files-of-specific-type.ps1 rename to 1 - Basics/count-files-of-specific-type/count-files-of-specific-type.ps1 diff --git a/1 - Basics/display-system-info.ps1 b/1 - Basics/display-system-info/display-system-info.ps1 similarity index 100% rename from 1 - Basics/display-system-info.ps1 rename to 1 - Basics/display-system-info/display-system-info.ps1 diff --git a/1 - Basics/list-files-in-folder.ps1 b/1 - Basics/list-files-in-folder/list-files-in-folder.ps1 similarity index 100% rename from 1 - Basics/list-files-in-folder.ps1 rename to 1 - Basics/list-files-in-folder/list-files-in-folder.ps1 diff --git a/1 - Basics/rename-files-in-folder-by-pattern.ps1 b/1 - Basics/rename-files-in-folder-by-pattern/rename-files-in-folder-by-pattern.ps1 similarity index 100% rename from 1 - Basics/rename-files-in-folder-by-pattern.ps1 rename to 1 - Basics/rename-files-in-folder-by-pattern/rename-files-in-folder-by-pattern.ps1 diff --git a/1 - Basics/to-do-text-file-through-script/to-do-text-file-through-script.ps1 b/1 - Basics/to-do-text-file-through-script/to-do-text-file-through-script.ps1 new file mode 100644 index 0000000..911d4a3 --- /dev/null +++ b/1 - Basics/to-do-text-file-through-script/to-do-text-file-through-script.ps1 @@ -0,0 +1,5 @@ +# Prompt the user and add their input to the textfile.txt +# Adding instead of setting so previous items are not lost + +$text = Read-Host "What item to you want to add to your to-do list? " +$text | add-Content $PSScriptRoot\textfile.txt \ No newline at end of file diff --git a/2 - Simple Automation/deleteOlderThan30Days/deleteOlderThan30Days.ps1 b/2 - Simple Automation/deleteOlderThan30Days/deleteOlderThan30Days.ps1 new file mode 100644 index 0000000..21673fe --- /dev/null +++ b/2 - Simple Automation/deleteOlderThan30Days/deleteOlderThan30Days.ps1 @@ -0,0 +1,14 @@ +# Deletes all objects older than 30 days inside the folder. +# -WhatIf Parameter was chosen for Remove-Item to remove the necessity for file -recreation +# Should creation of an file older than 30 days for testing purposes be necessary, run this script: + +# $file = "./testing.txt" +# New-Item -Path $file -ItemType File -Force +# $oldDate = (Get-Date).AddDays(-31) +# (Get-Item $file).LastWriteTime = $oldDate +# (Get-Item $file).LastAccessTime = $oldDate + +Get-ChildItem ".\" -Recurse | +Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | +Remove-Item -Force -WhatIf + diff --git a/1 - Basics/to-do-text-file-through-script.ps1 b/2 - Simple Automation/deleteOlderThan30Days/testing.txt similarity index 100% rename from 1 - Basics/to-do-text-file-through-script.ps1 rename to 2 - Simple Automation/deleteOlderThan30Days/testing.txt diff --git a/2 - Simple Automation/exportFolderFileListToCSV/exportFolderFileListToCSV.ps1 b/2 - Simple Automation/exportFolderFileListToCSV/exportFolderFileListToCSV.ps1 new file mode 100644 index 0000000..72ec2bc --- /dev/null +++ b/2 - Simple Automation/exportFolderFileListToCSV/exportFolderFileListToCSV.ps1 @@ -0,0 +1,5 @@ +# Export all file and folder path to folderFileList.csv +# Select-Object FullName property was chosen to only export the full path. +Get-ChildItem -Path "./" -Recurse | +Select-Object -Property FullName | +Export-Csv -Path "./folderFileList.csv" -NoTypeInformation \ No newline at end of file diff --git a/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Accounting/personell.csv b/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Accounting/personell.csv new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Accounting/salaries.csv b/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Accounting/salaries.csv new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Administration/personell.csv b/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Administration/personell.csv new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Administration/salaries.csv b/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Administration/salaries.csv new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Managers/personell.csv b/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Managers/personell.csv new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Managers/salaries.csv b/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Managers/salaries.csv new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Managers/upcomingPresentation.pptx b/2 - Simple Automation/exportFolderFileListToCSV/fictionalCompany/Managers/upcomingPresentation.pptx new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/fileFolderSorting/empty copy.png b/2 - Simple Automation/fileFolderSorting/empty copy.png new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/fileFolderSorting/empty.png b/2 - Simple Automation/fileFolderSorting/empty.png new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/fileFolderSorting/fileFolderSorting.ps1 b/2 - Simple Automation/fileFolderSorting/fileFolderSorting.ps1 new file mode 100644 index 0000000..4433834 --- /dev/null +++ b/2 - Simple Automation/fileFolderSorting/fileFolderSorting.ps1 @@ -0,0 +1,25 @@ +# Assign all relevant file-extensions to arrays to easily filter through them during a for-loop +# This approach was choosen for its simplicity and visibility + +[String[]]$videoFormat = "mp4", "mkv", "mov", "avi", "wmv", "flv", "webm", "mpeg", "mpg", "m4v", "3gp", "ogv", "ts", "vob" +[String[]]$pictureFormat = "jpg", "jpeg", "png", "gif", "bmp", "tiff", "tif", "webp", "heic", "heif", "raw", "cr2", "nef", "arw", "svg", "ico" +[String[]]$documentFormat = "pdf", "doc", "docx", "odt", "rtf", "txt", "csv", "xls", "xlsx", "ods", "ppt", "pptx", "odp", "md", "html", "xml" + +$filesInFolder = Get-ChildItem -Path "./" -File + +foreach($file in $filesInFolder){ + $extension = $file.Extension.TrimStart('.').ToLower() + + if ($videoFormat -contains $extension){ + Move-Item -Path $file.FullName -Destination "./videos/$($file.Name)" + } + + elseif ($pictureFormat -contains $extension) { + Move-Item -Path $file.FullName -Destination "./pictures/$($file.Name)" + } + + elseif ($documentFormat -contains $extension) { + Move-Item -Path $file.FullName -Destination "./documents/$($file.Name)" + } + +} \ No newline at end of file diff --git a/2 - Simple Automation/fileFolderSorting/pdf1 copy.pdf b/2 - Simple Automation/fileFolderSorting/pdf1 copy.pdf new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/fileFolderSorting/pdf1.pdf b/2 - Simple Automation/fileFolderSorting/pdf1.pdf new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/fileFolderSorting/test copy.mp4 b/2 - Simple Automation/fileFolderSorting/test copy.mp4 new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/fileFolderSorting/test.mp4 b/2 - Simple Automation/fileFolderSorting/test.mp4 new file mode 100644 index 0000000..e69de29 diff --git a/2 - Simple Automation/findAndReplaceTextMultipleFiles/findAndReplaceTextMultipleFiles.ps1 b/2 - Simple Automation/findAndReplaceTextMultipleFiles/findAndReplaceTextMultipleFiles.ps1 new file mode 100644 index 0000000..0bc4fb1 --- /dev/null +++ b/2 - Simple Automation/findAndReplaceTextMultipleFiles/findAndReplaceTextMultipleFiles.ps1 @@ -0,0 +1,8 @@ +# File searches for txt files and replaces specified string. + +$textFiles = Get-ChildItem -Path "./" -Filter *.txt +$searchText = 'this text is false' +$replaceText = 'this text is true' +foreach($textFile in $textFiles){ + (Get-Content $textFile) -replace $searchText, $replaceText | Set-Content $textFile +} \ No newline at end of file diff --git a/2 - Simple Automation/findAndReplaceTextMultipleFiles/sample.txt b/2 - Simple Automation/findAndReplaceTextMultipleFiles/sample.txt new file mode 100644 index 0000000..1f3eb38 --- /dev/null +++ b/2 - Simple Automation/findAndReplaceTextMultipleFiles/sample.txt @@ -0,0 +1,2 @@ +this text is false + diff --git a/2 - Simple Automation/findAndReplaceTextMultipleFiles/text1.txt b/2 - Simple Automation/findAndReplaceTextMultipleFiles/text1.txt new file mode 100644 index 0000000..1f3eb38 --- /dev/null +++ b/2 - Simple Automation/findAndReplaceTextMultipleFiles/text1.txt @@ -0,0 +1,2 @@ +this text is false + diff --git a/2 - Simple Automation/folderBackup/folderBackup.ps1 b/2 - Simple Automation/folderBackup/folderBackup.ps1 new file mode 100644 index 0000000..629850e --- /dev/null +++ b/2 - Simple Automation/folderBackup/folderBackup.ps1 @@ -0,0 +1,7 @@ +# Copy 'important' folder to 'safe' location +# Get-ChildItem -Filter parameter was deemed insufficient and replaced by -Directory | Where-Object +# As the -Filter parameter only matches folders at current level, not recursively in all subfolders. + +$nameOfFolder = "important" +$directionOfFolder = Get-ChildItem -Path "./" -Recurse -Directory | Where-Object { $_.Name -eq $nameOfFolder } +Copy-Item -Path $directionOfFolder.FullName -Destination (Join-Path "./safe/" $directionOfFolder.Name) -Recurse -Force diff --git a/2 - Simple Automation/folderBackup/unsafe/important/important.txt b/2 - Simple Automation/folderBackup/unsafe/important/important.txt new file mode 100644 index 0000000..fcfb68e --- /dev/null +++ b/2 - Simple Automation/folderBackup/unsafe/important/important.txt @@ -0,0 +1 @@ +important \ No newline at end of file diff --git a/3 - Intermediate/checkRunningProcessAndAlertIfIsNotRunning/checkRunningProcessAndAlertIfIsNotRunning.ps1 b/3 - Intermediate/checkRunningProcessAndAlertIfIsNotRunning/checkRunningProcessAndAlertIfIsNotRunning.ps1 new file mode 100644 index 0000000..6b71779 --- /dev/null +++ b/3 - Intermediate/checkRunningProcessAndAlertIfIsNotRunning/checkRunningProcessAndAlertIfIsNotRunning.ps1 @@ -0,0 +1,14 @@ +# Opens notepad process/program if it is not in the running Process list. +# processName variable for easy exchange of process +# Forms Messagebox instead of Write-Host for more urgency + +$processName = "notepad" + +while ($true){ + if (-not (Get-Process -name $processName -ErrorAction SilentlyContinue)){ + [System.Windows.Forms.MessageBox]::Show("$processName is not running! It will now be opened.", "Alert", + [System.Windows.Forms.MessageBoxButtons]::Ok, + [System.Windows.Forms.MessageBoxIcon]::Warning) + Start-Process $processName + } +} \ No newline at end of file diff --git a/3 - Intermediate/downloadFilesFromURL/downloadFilesFromURL.ps1 b/3 - Intermediate/downloadFilesFromURL/downloadFilesFromURL.ps1 new file mode 100644 index 0000000..a843f04 --- /dev/null +++ b/3 - Intermediate/downloadFilesFromURL/downloadFilesFromURL.ps1 @@ -0,0 +1,26 @@ +# Creates downloads Folder and uses the Invoke-WebRequest function to acquire and download file to said folder. +# System.IO.Path is called to extract Filename from URL. +# Microsoft.VisualBasic.Interaction is called to provide simple input dialog. + +$downloadFolder = "$($PSScriptRoot)/downloads" + +if (-not (Test-Path $downloadFolder)){ + New-Item -Path $downloadFolder -ItemType Directory +} + +Add-Type -AssemblyName Microsoft.VisualBasic +$userInput = [Microsoft.VisualBasic.Interaction]::InputBox("Enter URL: ", "downloadFilesFromURL", "") +# [System.Windows.Forms.MessageBox]::Show("Your entered URL is '$userInput'.", "Alert", +# [System.Windows.Forms.MessageBoxButtons]::Ok, +# [System.Windows.Forms.MessageBoxIcon]::Information) + +if (-not $userInput -or -not ($userInput -match "^https?://")){ + [System.Windows.Forms.MessageBox]::Show("Incorrect URL : $userInput", "Alert", + [System.Windows.Forms.MessageBoxButtons]::Ok, + [System.Windows.Forms.MessageBoxIcon]::Error) + return +} + +$fileName = [System.IO.Path]::GetFileName($userInput) +$downloadFolderPath = Join-Path $downloadFolder $fileName +Invoke-WebRequest -Uri $userInput -OutFile $downloadFolderPath -ErrorAction Stop diff --git a/3 - Intermediate/generateReportOfInstalledPrograms/generateReportOfInstalledPrograms.ps1 b/3 - Intermediate/generateReportOfInstalledPrograms/generateReportOfInstalledPrograms.ps1 new file mode 100644 index 0000000..037a45d --- /dev/null +++ b/3 - Intermediate/generateReportOfInstalledPrograms/generateReportOfInstalledPrograms.ps1 @@ -0,0 +1,10 @@ +# Exports CSV file containing all installed programms + +# Test-Path added to avoid Remove-Item error on first execution. +# Remove-Item was added to avoid complications with Export-Csv function. +# Delimiter was added to not have all properties stacked in the same column. +if (Test-Path "./InstalledPrograms.csv"){ +Remove-Item -Path "./InstalledPrograms.csv" +} +Get-Package | Select-Object -Property Name, Version, Summary | +Export-Csv -Path "./InstalledPrograms.csv" -NoTypeInformation -Delimiter ";" diff --git a/3 - Intermediate/monitorFolderAndLogChanges/happenings/aFileCreated.txt b/3 - Intermediate/monitorFolderAndLogChanges/happenings/aFileCreated.txt new file mode 100644 index 0000000..e69de29 diff --git a/3 - Intermediate/monitorFolderAndLogChanges/happenings/aFileDeleted.txt b/3 - Intermediate/monitorFolderAndLogChanges/happenings/aFileDeleted.txt new file mode 100644 index 0000000..e69de29 diff --git a/3 - Intermediate/monitorFolderAndLogChanges/monitorFolderAndLogChanges.ps1 b/3 - Intermediate/monitorFolderAndLogChanges/monitorFolderAndLogChanges.ps1 new file mode 100644 index 0000000..8fd4ddc --- /dev/null +++ b/3 - Intermediate/monitorFolderAndLogChanges/monitorFolderAndLogChanges.ps1 @@ -0,0 +1,39 @@ +# File creates listeners for happenings folder +# Listens for file creation, deletion, renaming +# Exports events to log.txt + +# $fsw to save typing for three listeners. +# Property @{IncludeSubdirectories = $true} to trigger listeners in subdirectories too. +# EnableRaisingEvents = $true explicitely enabled for completeness sake, by default powershell implicitely turns it on. +$fsw = New-Object IO.FileSystemWatcher "$($PSScriptRoot)/happenings/" -Property @{ + IncludeSubdirectories = $true + EnableRaisingEvents = $true} + +Register-ObjectEvent $fsw Created -SourceIdentifier "fileFolderCreated" -Action { + $name = $Event.SourceEventArgs.Name + $fullPath = $Event.SourceEventArgs.FullPath + + Add-Content "./log.txt" -value "[FILE CREATION]: DATE: $(Get-Date), NAME: $name, PATH: $fullPath" +} + +Register-ObjectEvent $fsw Deleted -SourceIdentifier "fileFolderDeleted" -Action { + $name = $Event.SourceEventArgs.Name + $fullPath = $Event.SourceEventArgs.FullPath + + Add-Content "./log.txt" -value "[FILE DELETION]: DATE: $(Get-Date), NAME: $name, PATH: $fullPath" +} + +Register-ObjectEvent $fsw Renamed -SourceIdentifier "fileFolderRenamed" -Action { + $oldName = $Event.SourceEventArgs.OldName + $oldFullPath = $Event.SourceEventArgs.OldFullPath + $name = $Event.SourceEventArgs.Name + $fullPath = $Event.SourceEventArgs.FullPath + + Add-Content "./log.txt" -value "[FILE RENAMING]: DATE: $(Get-Date), OLDNAME: $oldName, OLDPATH: $oldFullPath, NEWNAME: $name, NEWPATH: $fullPath" +} + +# See active listeners +# Get-EventSubscriber -Force | Format-Table -AutoSize + +# Remove all active listeners +# Get-EventSubscriber -Force | Unregister-Event -Force