mirror of
https://github.com/serialexperiments0815/PowershellExcercises.git
synced 2026-07-12 15:32:25 +00:00
Expanded and structurized scripts.
This commit is contained in:
parent
361e384195
commit
fbcf753965
33 changed files with 158 additions and 0 deletions
|
|
@ -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
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
|
|
|
|
|
|
|
|
|
0
2 - Simple Automation/fileFolderSorting/empty copy.png
Normal file
0
2 - Simple Automation/fileFolderSorting/empty copy.png
Normal file
0
2 - Simple Automation/fileFolderSorting/empty.png
Normal file
0
2 - Simple Automation/fileFolderSorting/empty.png
Normal file
|
|
@ -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)"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
0
2 - Simple Automation/fileFolderSorting/pdf1 copy.pdf
Normal file
0
2 - Simple Automation/fileFolderSorting/pdf1 copy.pdf
Normal file
0
2 - Simple Automation/fileFolderSorting/pdf1.pdf
Normal file
0
2 - Simple Automation/fileFolderSorting/pdf1.pdf
Normal file
0
2 - Simple Automation/fileFolderSorting/test copy.mp4
Normal file
0
2 - Simple Automation/fileFolderSorting/test copy.mp4
Normal file
0
2 - Simple Automation/fileFolderSorting/test.mp4
Normal file
0
2 - Simple Automation/fileFolderSorting/test.mp4
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
this text is false
|
||||||
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
this text is false
|
||||||
|
|
||||||
7
2 - Simple Automation/folderBackup/folderBackup.ps1
Normal file
7
2 - Simple Automation/folderBackup/folderBackup.ps1
Normal file
|
|
@ -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
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
important
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
|
@ -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 ";"
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in a new issue