Here you can find my PowerShell scripts for various purposes. They should run both on Windows and PowerShell Core on Linux (unless they use some Windows-specific APIs). Please be informed that I did not test them on Linux much or at all in some cases.
To unpack 7z archives you will need a free 7-zip utility program.
There are two sections available below - tools and games.
Tools
A simple tool to encode and decode files using base64 encoding. Works on Windows only, because it is using certutil.exe which is a standard part of Windows 10 installation.
Usage:
input folder - here you should put files which you want to encode
encoded folder - here will all your encoded files appear
decoded folder - this is where all your decoded files will be (script picks files from encoded folder)
pwsh_encode_file_v2.ps1 source:
Remove-Item "./encoded/*"
Remove-Item "./decoded/*"
$files = Get-ChildItem -path "./input/*"
ForEach ($file in $files){
$filename = $file.BaseName+$file.Extension
Write-Host "---------------------"
Write-Host "Encoding: "$filename
Write-Host ""
certutil -encode ("./input/"+$filename) ("./encoded/"+$filename+".b64")
Write-Host ($filename+".b64")"file Encoded Successfully!"
Write-Host "---------------------"
}
Write-Host ""
Read-Host "Press enter to exit"
pwsh_decode_file_v2.ps1 source:
$files = Get-ChildItem -path "./encoded/*"
ForEach ($file in $files){
$filename = $file.BaseName+$file.Extension
Write-Host "---------------------"
Write-Host "Decoding: "$filename
Write-Host ""
certutil -decode ("./encoded/"+$filename) ("./decoded/"+$filename.Substring(0,$filename.Length-4))
Write-Host ($filename)"file decoded Successfully!"
Write-Host "---------------------"
}
Remove-Item "./encoded/*"
Write-Host ""
Read-Host "Press enter to exit"
This is a tool to merge and split PDF files. It uses a PSWritePDF module which needs to be installed first before running the script.
Usage:
Before first run:
Open PowerShell as Admin and then issue the command: Install-Module -Name PSWritePDF
Then:
1) Prepare files:
Input folder - place pdf file to split or place pdf files to merge (they go in alphabetical order when merged)
Output folder - there will be your result.
2) Run the script via start.bat
start.bat source:
powershell.exe -ExecutionPolicy unrestricted -NoExit -command "Start-Process powershell ./pdf-tools.ps1"
pdf-tools.ps1 source:
$global:extension_pdf = ".pdf"
$global:path_output = $PSScriptRoot.ToString()+"\output\"
$global:path_input = $PSScriptRoot.ToString()+"\input\"
function menu{
Clear-Host
Write-Host "Welcome to PDF-Tools"
Write-Host ""
Write-Host "Menu:"
Write-Host "1. Merge PDF files into one"
Write-Host "2. Split PDF files into separate pages"
Write-Host "3. Exit program"
Write-Host ""
$choice = Read-Host "Selection: [1],[2],[3]"
if ($choice -eq 1){
merge
}
if ($choice -eq 2){
split
}
if ($choice -eq 3){
Exit
}
else {
Write-Host "Error... incorrect input"
}
Read-Host "Press any key to continue"
}
function merge{
$files = (Get-ChildItem $path_input -File).FullName
Write-Host ($files -join "`n")
Write-Host "This may take a while... be patient"
$file_name = Get-Random -Minimum 0 -Maximum 100000000
Merge-PDF -InputFile $files -OutputFile "$path_output\$file_name$extension_pdf"
Read-Host "Done. Press any key to leave program"
Exit
}
function split{
$file_name = Read-Host "Please enter filename (without extension) which you want to be split: "
Write-Host "This may take a while... be patient"
Split-PDF -FilePath "$path_input$file_name$extension_pdf" -OutputFolder "$path_output"
Read-Host "Done. Press any key to leave program"
Exit
}
while ($true){
menu
}
A simple network interface traffic monitoring tool. Shows how many GB were downloaded/uploaded with a given update threshold. I did not test it on Linux, but I think it will only run on Windows as from what I recall I was not very successful with Get-NetAdapter command on Linux on which this tool is based.
netstat.ps1 source:
Get-NetAdapter
$ifIndex = Read-Host "Enter ifIndex of network adapter you want to monitor"
$interval = Read-Host "Enter interval in seconds between the stats will update"
while ($true){
Clear-Host
$ReceivedBytes = Get-NetAdapter -ifIndex $ifIndex | Get-NetAdapterStatistics | select ReceivedBytes
$SentBytes = Get-NetAdapter -ifIndex $ifIndex | Get-NetAdapterStatistics | select SentBytes
$ReceivedBytes = $ReceivedBytes -replace "[^0-9]" , ""
$SentBytes = $SentBytes -replace "[^0-9]" , ""
$ReceivedGBytes = $ReceivedBytes / (1024 * 1024 * 1024)
$SentGBytes = $SentBytes / (1024 * 1024* 1024)
Write-Host "Downloaded "$ReceivedGBytes" GB"
Write-Host "Sent "$SentGBytes" GB"
Start-Sleep -seconds $interval
}
A simple Windows tool to monitor CPU usage and how much RAM is free.
syschk.ps1 source:
$interval = Read-Host "Enter interval in seconds between the stats will update"
while ($true){
Clear-Host
$cpu_usage = Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select Average
$cpu_usage = $cpu_usage -replace "[^0-9]" , ""
$ram_usage = Get-CIMInstance Win32_OperatingSystem | Select FreePhysicalMemory
$ram_usage = $ram_usage -replace "[^0-9]" , ""
Write-Host "CPU usage: "$cpu_usage" %"
Write-Host "RAM free: "$($ram_usage/1000)" MB"
Start-Sleep -seconds $interval
}
A simple tool to take a full screen screenshot and save it to file at given interval. The script will stop running at a time user will set in the script or when there is less than 30GB free on drive C: For Windows only.
Usage:
screens folder - this is where all your screenshots will appear
screen.ps1 - main script
screen.ps1 source:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$Width = $Screen.Width
$Height = $Screen.Height
$Left = $Screen.Left
$Top = $Screen.Top
$path = $PSScriptRoot.ToString()+"\screens\"
$number_taken = 0
$interval = 30
Clear-Host
Write-Host "The script will stop running if there is less that 30GB space left on C drive"
Write-Host "It is thus advisable to run this script from anywhere on drive C"
Start-Sleep -Seconds 5
function Set-Interval($interval_s) {
Read-Host "Please input interval in seconds between screenshots: [integer number]"
}
$interval = Set-Interval
$test = $interval -match '^[0-9]+$'
$time_stop = Read-Host "What time should the screenshoter stop working? [yyyyMMdd_HHmmss_fff]"
$power_off = Read-Host "Should your computer be turned off afterwards? [Y/N]"
if ($test.ToString() -eq "False")
{
Write-Host "Interval must be an integer number of seconds"
Start-Sleep -Seconds 2
}
else
{
do
{
$file_name = Get-Random -Minimum 0 -Maximum 100000000
$time_stamp = Get-Date -Format "yyyyMMdd_HHmmss_fff"
$time_stamp2 = $time_stamp
$path = $path.ToString()
$file_name = $file_name.ToString()+".png"
$time_stamp = $time_stamp.ToString()+"_"
$file_name = $time_stamp+$file_name
$bitmap = New-Object System.Drawing.Bitmap $Width, $Height
$graphic = [System.Drawing.Graphics]::FromImage($bitmap)
$graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)
$bitmap.Save($path+$file_name)
$file_size = [INT]((Get-Childitem -Path $path$file_name).Length / 1KB)
$number_taken = $number_taken+1
Write-Host "================================================================================"
Write-Host "[$($number_taken.ToString())] Screenshot saved to:"
Write-Host $path$file_name
Write-Host "Filesize: "$file_size" KB"
$free_space = Get-PSDrive C
Write-Host "Free space left on drive C: "($free_space.Free / 1KB).ToString()"KB"
if (($free_space.Free / 1KB) -le 30000000)
{
Break
}
Write-Host "================================================================================"
Write-Host "Waiting $($interval.ToString()) seconds..."
Start-Sleep -Seconds $interval
Write-Host "Current timestamp: "$time_stamp2.ToString()
Write-Host "Script will run until: "$time_stop.ToString()
if ($power_off -eq "Y")
{
Write-Host "System will shut down once the target is reached"
}
} while ($time_stamp2 -le $time_stop)
if ($power_off -eq "Y")
{
Write-Host "System will power down now"
Write-Host "Written "$number_taken.ToString()" screenshots"
Start-Sleep -Seconds 2
Stop-Computer
}
else
{
Write-Host "Done"
Write-Host "Written "$number_taken.ToString()" screenshots"
}
}
Games
Warning this game has many profanities!
This is a very early stage of a text based adventure game with randomly generated rooms. It is easily expandable using plain txt files. So far only room generation and walking around is implemented.
Usage:
data folder - this is where all the words are stored. File structure is self-explanatory so I will not get much deeper into it
poweruserdung.ps1 - main script
poweruserdung.ps1 source:
$global:room_size = Get-Content .\data\room_sizes.txt
$global:room_shape = Get-Content .\data\room_shapes.txt
$global:room_type = Get-Content .\data\room_types.txt
$global:room_exits = Get-Content .\data\room_exits.txt
$global:room_contents = Get-Content .\data\room_contents.txt
$global:room_areas = Get-Content .\data\room_areas.txt
$global:room_floors = Get-Content .\data\room_floors.txt
$global:room_fragnances = Get-Content .\data\room_fragnances.txt
$global:room_walls = Get-Content .\data\room_walls.txt
$global:general_adjectives = Get-Content .\data\general_adjectives.txt
$global:room_size_count = 0
$global:room_shape_count = 0
$global:room_type_count = 0
$global:room_exits_count = 0
$global:room_contents_count = 0
$global:room_areas_count = 0
$global:room_floors_count = 0
$global:room_fragnances_count = 0
$global:room_walls_count = 0
$global:general_adjectives_count = 0
function game_init {
Clear-Host
Write-Host "Loadingu!!!!!1one"
Start-Sleep -Milliseconds 400
foreach ($item in $room_size) {$room_size_count++}
foreach ($item in $room_shape) {$room_shape_count++}
foreach ($item in $room_type) {$room_type_count++}
foreach ($item in $room_exits) {$room_exits_count++}
foreach ($item in $room_contents) {$room_contents_count++}
foreach ($item in $room_areas) {$room_areas_count++}
foreach ($item in $room_floors) {$room_floors_count++}
foreach ($item in $room_fragnances) {$room_fragnances_count++}
foreach ($item in $room_walls) {$room_walls_count++}
foreach ($item in $general_adjectives) {$general_adjectives_count++}
game_menu
}
function load_game{
Write-Host "Not implemented yet."
Read-Host
game_menu
}
function scores{
Write-Host "Not implemented yet."
Read-Host
game_menu
}
function game_menu{
while($true){
Clear-Host
Write-Host "Welcome to PowerUser Dungeon. A random game about tesseract exploration.`n`n"
Write-Host "There are" $room_size_count" different room sizes in the database."
Write-Host "There are" $room_shape_count" different room shapes in the database."
Write-Host "There are" $room_type_count" different room types in the database."
Write-Host "There are" $room_contents_count" different room contents in the database."
Write-Host "There are" $room_areas_count" different room areas in the database."
Write-Host "There are" $room_floors_count" different room floors in the database."
Write-Host "There are" $room_fragnances_count" different room fragnances in the database."
Write-Host "There are" $room_walls_count" different room walls in the database."
Write-Host "There are" $general_adjectives_count" different adjectives in the database."
Write-Host "There are" $room_exits_count" different room exits in the database.`n"
Write-Host "Currently there are"($room_size_count*$room_shape_count*$room_type_count*`
$room_contents_count*$room_areas_count*$room_floors_count*$room_fragnances_count*$room_walls_count`
*$general_adjectives_count*$room_exits_count*$general_adjectives_count)"possible tesseract configurations.`n"
Write-Host "=========Game=Menu=========`n"
Write-Host "1 Start Game"
Write-Host "2 Load Game"
Write-Host "3 Scores"
Write-Host "4 Quit`n"
$selection = Read-Host "Please enter your choice: "
if($selection -eq 1){game_main}
if($selection -eq 2){load_game}
if($selection -eq 3){scores}
if($selection -eq 4){Exit}
else{Write-Host "Wrong selection";Start-Sleep -Seconds 2}
}
}
function game_main{
while($true){
Clear-Host
$choice="A"
Write-Host "You are standing in a"(Get-Random -InputObject $room_size)`
(Get-Random -InputObject $room_shape)`
(Get-Random -InputObject $room_type)"with"(Get-Random -InputObject $general_adjectives)`
(Get-Random -InputObject $room_walls)"There is"(Get-Random -InputObject $room_contents)`
(Get-Random -InputObject $room_areas)
Write-Host "The floor is made of"(Get-Random -InputObject $general_adjectives)`
(Get-Random -InputObject $room_floors)"The whole area"(Get-Random -InputObject $room_fragnances)
$room_ex=(Get-Random -InputObject $room_exits)
Write-Host "`nPossible exit is "$room_ex
$choice = Read-Host "Please input your command: "
if($choice -eq $room_ex){game_main}
else{Write-Host "`nDespite of no obvious way of going the direction you choose, you went"$choice".";
Write-Host "`rMoments later you realized you ended up in a dark void of interspatial nothingness where you gonna spend the rest of your days.";
Read-Host;game_menu}
}
}
game_init
This is a very early prototype of a game? a simulator? Well, a something that would simulate very early days of how I imagine 80s net could have looked like in some alternative universe.
Usage:
help folder - here is a simple help, accessible from the script as well
pages folder - here are "webpages" they are in TXT form and you can easily add your own content and new pages as well. You do not need to change anything in the code - script will autoupdate its website directory and available commands to access them
read_page.ps1 - main script which you should run
any available commands are explained in script by executing help command
read_page.ps1 source:
$global:path = $PSScriptRoot.ToString()+"\pages\"
$global:block_filename = "block.txt"
function read_page ($page){
$page_name = $page
$lines = Get-Content -Path $global:path$page_name -Raw
$block_char = Get-Content -Path $global:path$global:block_filename -Raw -Encoding utf8
$interpreted = $lines -replace "#", $block_char
Write-Host ""
Write-Host $interpreted
Write-Host ""
}
function web_directory{
Write-Host "Currently there are following pages available online: "
Write-Host ""
Get-ChildItem $global:path -Exclude $global:block_filename | ForEach-Object -Process {[System.IO.Path]::GetFileNameWithoutExtension($_)}
Write-Host ""
}
function browser{
$page = Read-Host "Please enter the webpage name you want to visit: "
$page = $page+".txt"
Write-Host $page
Read-Host "Press any key to continue..."
read_page($page)
}
function help{
$help_path = $PSScriptRoot.ToString()+"\help\"
$help_txt ="help_site.txt"
$lines = Get-Content -Path $help_path$help_txt -Raw
Write-Host $lines
Write-Host ""
}
while ($true){
Clear-Host
Write-Host "Welcome to NECOnet v0.5. Available commands are: browser, netdir, help, exit"
Write-Host ""
$command = Read-Host "Command: "
if ($command -eq "browser"){
browser
}
if ($command -eq "help"){
help
}
if ($command -eq "exit"){
Exit
}
if ($command -eq "netdir"){
web_directory
}
Read-Host "Press any key to continue..."
}
This is it for now. Remember you are using these scripts at your own risk! Feel free to get in touch should you have any questions about running these.