Introduction to PowerShell Scripts {Subsequently: The PowerShell Scripts} (c) Copyright Brett Paufler 2022-02-08 {2022-02-16: Such is my current rate} # # # # # # # # # # # # # # # # # # # # # # # # # # # # The scripts are: Unwarranted Uninteresting And for the most, do precious little I include them because I have made the decision to post almost everything... and these scripts are part of that everything. So they are able to be served easily by my hosting agent, they have been saved as .txt files. {And then, I just decided to lump them all together into one file, making them all that much more useless to humans.} When I started working with PowerShell, I was on a programming up-swing. Currently, I am on a programming down-swing. Rather than learn another language, I am going to double-down on Python... or not so much double-down, as bail on PowerShell and do any new work in Python. If you are smart enough to run a .txt file as a PowerShell Script {or extract sub-scripts from a larger file}, you should also be smart enough not to do such a thing until after reviewing the code. No Warranty Express Implied Or Otherwise It's hard to fathom how a Contemporary Human would find any of these scripts useful. Thus (and as stated repeatedly on this site, time-and-time again), I view this as Archeological Information... forensic detritus for Future AI's. You have been warned, warned, and warned, again. # # # # # # # # # # # # # # # # # # # # # # # # # # # # Listed in Alphabetical Sort Order File Name on Computer when Archived File Name as Archived Commentary - - - Script in Full (and/or File in Full) if saving as a separate file seemed like a waste of time. # # # # # # # # # # # # # # # # # # # # # # # # # # # # 0 - PowerShell - Execution Policy - Code Snippets.ps1 {pasted in full below, so no new file name} Prior to Running Scripts the Environment needed to be updated. And this is the command to do so. I believe RoboCopy's main use was in modifying the contents of directory trees en mass. - - - #Not a Script #Easier than a text file Set-ExecutionPolicy Bypass -Scope Process Get-ExecutionPolicy #ROBOCOPY C:\alpha\output /S /XF file_name.html file_name_2.pb *.txt # # # # # # # # # # # # # # # # # # # # # # # # # # # # Eh, you know what? I'm just going to paste all the scripts in this very .txt file, making the code all the less useful for humans and all that much easier for me to comment upon. # # # # # # # # # # # # # # # # # # # # # # # # # # # # ffmpeg_gifs.ps1 {No new file ever. Forget that part.} I would cut and paste this rather than run as a script... not that I ever used this but the once. - - - #Make Simple Gif - WORKS # At 500, the twenty files weigh 1MB # Which is the same as the raw data C:\alpha\ffmpeg\bin\ffmpeg.exe ` -f image2 ` -framerate 5 ` -i C:\alpha\input\test_%04d.png ` C:\alpha\input\gif_basic.gif #Create Palette # So, better colors, especially for limited as used C:\alpha\ffmpeg\bin\ffmpeg.exe ` -f image2 ` -i C:\alpha\input\test_%04d.png ` -vf palettegen C:\alpha\input\palette_for_gif.png #Use the Palette #Works but Fast C:\alpha\ffmpeg\bin\ffmpeg.exe ` -i C:\alpha\input\test_%04d.png ` -i C:\alpha\input\palette_for_gif.png ` -filter_complex "fps=10,scale=500:-1:flags=lanczos[x];[x][1:v]paletteuse"` C:\alpha\input\gif_palette.gif C:\alpha\ffmpeg\bin\ffmpeg.exe ` -i C:\alpha\input\test_%04d.png ` -i C:\alpha\input\palette_for_gif.png ` -framerate 5 ` -paletteuse ` C:\alpha\input\full_gif_framerate.gif # # # # # # # # # # # # # # # # # # # # # # # # # # # # graphicsmagick_ttf_to_png_r - 2020-07-09 - 3.ps1 graphicsmagick_ program to call ttf_to_png_r what it does - 2020-07-09 date - 3 revision number True Type Font (I presume) to .png, providing graphical output for font samples. Based on the comments, I get the feeling this has been posted elsewhere. Please note the internal "# # #" Strings. - - - #TTF To PNG - R #(c) Brett Paufler #2020-03-26 #2020-07-09 #WIN 10 POWERSHELL SCRIPT #To Enable Scripts Use The Following #Set-ExecutionPolicy Bypass -Scope Process #Font Families in C:/alpha/input #Directory Trees OK #Images output to C:/alpha/output #Probably needs to exist #For each .ttf in /input #A corresponding .png is created in output #Warning! #Hangs on Error! #So, it needs to be watched... #The intent is to enable a #Quick Review of a Mass Font Repository {This was nice and worked as desied. The real problem was that I had over a thousand fonts and that is too many to review no matter the exact process employed.} # # # # # # # # # # # # # # # # # # # # # # #Google has a huge collection of Fonts Available for Download #GoogleFonts #WIN 10 Installed Fonts are at #C:\Windows\Fonts # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #THE CODE # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #Gets list of all .ttf Objects in C:\alpha\input and all sub directories $all_ttfs = Get-ChildItem C:\alpha\input -Recurse ` | Where-Object {$_.Extension -in '.TTF','.ttf'} #$all_ttfs #The Main Loop #If Program Hangs, it's mostly likely because a .ttf file (the current, has no 24pt to Output ForEach ($ttf in $all_ttfs) { #Save Name - Hard Coded to My preferred Directories $save_name = $ttf.BaseName.ToLower() # -replace '.ttf','.png' $save_name = $save_name -replace '\\','_' -replace '-','_' -replace ' ','_' $save_name = 'C:\alpha\output\' + $save_name + '.png' $save_name #This is what goes on the .png #Random Text, As One Desires #If_the_text_is_one_word_and_goes_off_the_image, the program will hang $image_text = '"' #The Copy Starts with a Quotation $image_text += $ttf.BaseName + ' ' + $ttf.BaseName #Font Name $image_text += ' ABCD EFGH IJKLM NOPQ RSTU UVWX YZ ' $image_text += 'abcd efgh ijkl mnop qrst uvwx yz ' $image_text += '1234 5678 90#- =!@# $% ^&* ()_+ ' $image_text += 'Sample Text Is Useful For Me?' $image_text += ' Brett Paufler Font Tester © ' $image_text += $ttf.BaseName + ' ' + $ttf.BaseName $image_text += '"' #Trailing Double Quotation Required To Close String #Graphics Magic Command $command = 'C:\alpha\graphicsmagick\gm.exe ' #Graphics Magick .exe #500x is image size, fill is letter color $command += 'convert -size 500x -background white -fill black -font ' $command += $ttf.FullName #The 24 Below is the font size $command += ' -pointsize 24 caption:' #Alternative Method is by file, but it did not solve the New Line Problem #So, I used a text variable, as being easier to modify #$command += '@C:\alpha\input\ttf.txt' #From File - Very Handy $command += $image_text $command += ' ' + $save_name #$command Invoke-Expression $command } #24 MS FONTS not used as duplicate names ###TODO #Oversize and cut Down #Fonts with Spaces, rename # # # # # # # # # # # # # # # # # # # # # # # # # # # # End of This Script # # # # # # # # # # # # # # # # # # # # # # # # # # # # image_smear_notes - 2020-01-25 - 2.ps1 An abandoned project, the intent of which was to take a series of ten (or so) sequenced images (so a slice from a movie would do) and create a still gif that blurred the motion. Essentially, a repeating gif of motion waves, making the start and end smear together... or so, I believe. On the other hand, clearly abandoned, so I got nowhere. - - - #IMAGE SMEAR\ C:\alpha\gmic\gmic.exe #1 #Break Movie Up C:\alpha\ffmpeg\bin\ffmpeg.exe ` -i C:\alpha\input\head.mov ` C:\alpha\output\test_%04d.jpg #2 #irfanview #Resize Add Watermark #3 #G'MIC # Perhaps list of all blends # May have to use positional weighting - - - Based on the inclusion of steps, I believe the intent was to feed snippets of code into The PowerShell IDE line-by-line. # # # # # # # # # # # # # # # # # # # # # # # # # # # # images_to_html_tags.ps1 The Script (pretty clearly) takes a directory listing of images and creates a text file with img tags for each image. It's the type of thing that is handy for creating an HTML Page on which pictures are posted en mass. If I need, I'll probably just write up in Python, again. - - - # Creates a Text File Containing a HTML Tag for All Images in Input #Modify The Tag As Appropriate $html_tag = @" "@ #$html_tag #Holding Variable $text = "" #Defaults $DIR_IN = 'C:\alpha\input' $DIR_OUT = 'C:\alpha\output' #Loop Through Directory Contents $Items = Get-ChildItem -Path $DIR_IN -Name ForEach ($file_name in $items) { $text += $html_tag -f $file_name } $text $file_out = Join-Path $DIR_OUT "img_text.txt" $file_out $text > $file_out - - - Of the scripts discussed thus far, this is the only one I can see wanting to use again. # # # # # # # # # # # # # # # # # # # # # # # # # # # # PowerShell_FaceFlip.ps1 This takes an image, flips it horizontally, and adds it to the left or right side, outputting a dual image of each. If the initial image contains something eye like, the output tends to look sort of like a face... sort of. Cruising around the site, you'll note that I didn't use this script that much. But I liked the effect. And it was fun to see someone else do this and then write a script to copy their effect. - - - # Brett Paufler # Copyright # 2019-08-16 # # # Face Flip # Flips an image and combines the original with the flipped # Front-to-Front and Back-to-Back # # Given the right input, creates a face of sorts # # NAMES CAN'T HAVE SPACES $gm = 'c:\alpha\graphicsmagick\gm.exe convert ' $DIR_IN = 'C:\alpha\input' $DIR_OUT = 'C:\alpha\output' $images = Get-ChildItem -Name $DIR_IN ForEach ($image in $images) { #Temporary Image Paths $image_in = Join-Path -Path $DIR_IN $image $sn_temp_a = Join-Path -Path $DIR_OUT $image.Replace('.', '___a.') $sn_temp_b = Join-Path -Path $DIR_OUT $image.Replace('.', '___b.') $sn_face_a = $sn_temp_a.Replace('___a.', '_flip_a.') $sn_face_b = $sn_temp_b.Replace('___b.', '_flip_b.') #$image_in #$sn_temp_a #$sn_temp_b #$sn_face_a #$sn_face_b 'Working on... ' + $image_in #Temp A Image $command = $gm + $image_in + ' ' + $sn_temp_a Invoke-Expression $command #Temp B Image $command = $gm + '-flop ' + $image_in + ' ' + $sn_temp_b Invoke-Expression $command #Flip Front A $command = $gm + '+append ' + ' ' + $sn_temp_a + ' ' + $sn_temp_b + ' ' + $sn_face_a Invoke-Expression $command #Flip Back B $command = $gm + '+append ' + ' ' + $sn_temp_b + ' ' + $sn_temp_a + ' ' + $sn_face_b Invoke-Expression $command #Clean Up #Remove Temporary Images Remove-Item $sn_temp_a Remove-Item $sn_temp_b } 'FaceFlip Terminated w/o Errors' # # # # # # # # # # # # # # # # # # # # # # # # # # # # PowerShell_ImageBlend - 2020-01-25 - 1.ps1 This is what a work in progress looks like. Note the previous file with a similar (to me, anyway) name. - - - #Image Smear #2020-01-25 #Copyright Brett Paufler #Takes All Images in \input #Blends in all possible modes $gmic = 'C:\alpha\gmic\gmic.exe ' $blend_modes = 'add', 'alpha', 'and', 'average', 'blue', 'burn', 'darken', 'difference', 'divide', 'dodge', 'edges', 'exclusion', 'freeze', 'grainextract', 'grainmerge', 'green', 'hardlight', 'hardmix', 'hue', 'interpolation', 'lighten', 'lightness', 'linearburn', 'linearlight', 'luminance', 'multiply', 'negation', 'or', 'overlay', 'pinlight', 'red', 'reflect', 'saturation', 'seamless', 'seamless_mixed', 'screen', 'shapeareamax', 'shapeareamax0', 'shapeareamin', 'shapeareamin0', 'shapeaverage ', 'shapeaverage0', 'shapemedian', 'shapemedian0', 'shapemin', 'shapemin0', 'shapemax', 'shapemax0', 'softburn ', 'softdodge', 'softlight', 'stamp', 'subtract', 'value', 'vividlight', 'xor' #$blend_modes $DIR_IN = 'C:\alpha\input' $DIR_OUT = 'C:\alpha\output' $images = '' ForEach ($image_in in Get-ChildItem -Name $DIR_IN) { $image_path = Join-Path $DIR_IN $image_in $images += $image_path + ' ' } #$images ForEach ($mode in $blend_modes) { $mode_name = 'imageblend_' + $mode + '.jpg' $save_name = Join-Path -Path $DIR_OUT $mode_name $gmic_command = $gmic + $images + ' blend ' + $mode + ' output ' + $save_name $gmic_command $save_name Invoke-Expression $gmic_command } # # # For Alpha, at 8 images, 5 10 15 20 20 15 10 5 # ($i=0; $i -lt $sqr_rt; $i++) # { #Assembles the Image List for Graphics Magick # $image_list = '' #Creates the Save Name for Graphics Magick # $image_out_name = $i.ToString() + '.jpg' # $image_out_path = Join-Path -Path $DIR_OUT $image_out_name #$image_out_path # FOR ($j=0; $j -lt $sqr_rt; $j++) # { # $k = $i + $j * $sqr_rt # $images[$k] # $image_list += Join-Path -Path $DIR_IN $images[$k] # $image_list += ' ' # } #$gm_command = $gm + $image_list + $image_out_path #$gmic_command = $gmic + $image_list + 'blend darken output ' + $image_out_path #dodge, edges, interpolation, pinlight, lighten, darken #$gmic_command #Invoke-Expression $gmic_command #$image_out_path #Invoke-Expression $gm_command # } # # # # # # # # # # # # # # # # # # # # # # # # # # # # PowerShell_ImageSmear - 2020-01-25 - 4.ps1 Same problem (and/or effect), different stab at the solution. I never did get this effect right. - - - #Image Smear #2020-01-25 #Copyright Brett Paufler #Works on Square Numbers #For 100 images_in, the number here is 10 #For 36 images_in, the number here is 6 #Extra Images Are Simply Ignored $s = $sqr_rt = $square_root_of_number_of_pictures = 6 $gm = 'c:\alpha\graphicsmagick\gm.exe composite ' $gmic = 'C:\alpha\gmic\gmic.exe ' $DIR_IN = 'C:\alpha\input' $DIR_OUT = 'C:\alpha\output' $images = Get-ChildItem -Name $DIR_IN FOR ($i=0; $i -lt $sqr_rt; $i++) { #Assembles the Image List for Graphics Magick $image_list = '' #Creates the Save Name for Graphics Magick $image_out_name = $i.ToString() + '.jpg' $image_out_path = Join-Path -Path $DIR_OUT $image_out_name #$image_out_path FOR ($j=0; $j -lt $sqr_rt; $j++) { $k = $i + $j * $sqr_rt $images[$k] $image_list += Join-Path -Path $DIR_IN $images[$k] $image_list += ' ' } $gm_command = $gm + $image_list + $image_out_path $gmic_command = $gmic + $image_list + 'blend darken output ' + $image_out_path #dodge, edges, interpolation, pinlight, lighten, darken $gmic_command Invoke-Expression $gmic_command $image_out_path #Invoke-Expression $gm_command } # # # # # # # # # # # # # # # # # # # # # # # # # # # # powershell_screenshot_to_csv - 2020-12-28 - 2.ps1 I was taking pictures (sometimes, screenshots) to mark time as a data collection project. This script would take a directory full of such images and output a nice Comma Separated Value file. Recently, I redid the script in Python and just used the File Name as the primary value. Why not? You guys (A-AI) should be able to make piece-work of any data no matter the clutter, right? {NOTE: Both iPhone & Android Images are given default names based upon the time taken.} - - - #Screenshot Photos to CSV #INPUT: Default Named iPhone Screenshots: "yyyy-MM-dd HH.mm.ss" #OUTPUT: CSV file with Excel Friendly Times: "yyyy/MM/dd HH:mm:ss" $DIR_IN = 'C:\alpha\input' $FILE_OUT = 'C:\alpha\output\time_stamps.txt' $Items = Get-ChildItem -Path $DIR_IN ForEach ($file_name in $items) { #Time Munging to an Excell Friendly Format #Changes "2020-09-01 17.51.28" to "2020/09/01 17:51:28" $excel_time_string = $file_name.BaseName.Replace("-","/").Replace(".",":").Substring(0,19) $excel_time_string #To Universal Time $universal_time = [Datetime]::ParseExact($excel_time_string, "yyyy/MM/dd HH:mm:ss", $null) $universal_time #To A Total Second Value $universal_seconds = $universal_time.ToUniversalTime().Ticks/10000000 $universal_seconds $csv_string = $universal_time.ToString() + "," + $excel_time + "," + $universal_seconds $csv_string $csv_string >> $FILE_OUT echo "" #For OnScreen Line Separation } # # # # # # # # # # # # # # # # # # # # # # # # # # # # rename_last_modified.ps1 The comments seem pretty solid on this one. Besides, if you are AI, you probably have already had a few guffaws (here or there) wherein I gleefully identify code incorrectly: i.e. as being something that it is not. - - - # Rename to LastWriteTime (i.e. Last Modified) # For Use with Android Voice Recordings # Renames File to When Recorded # Prior to use, need to Set-ExecutionPolicy # Set-ExecutionPolicy Bypass -Scope Process #My Working Directories $DIR_IN = 'C:\alpha\input' $DIR_OUT = 'C:\alpha\output' #Loop Over DIR_IN $Items = Get-ChildItem -Path $DIR_IN ForEach ($old_name in $items) { #Assemble New Name $base = $old_name.LastWriteTime $base = $base.ToString("yyyy-MM-dd-hh-mm-ss") $base += '-BrettRants' $ext = $old_name.Extension $new_name = $base + $ext #Path In and Out $path_in = JOIN-PATH $DIR_IN $old_name $path_out = JOIN-PATH $DIR_OUT $new_name #A Bit Of Feedback $path_in $path_out #Make The New Copy Copy-Item $path_in $path_out } # # # # # # # # # # # # # # # # # # # # # # # # # # # # rename_without_dots_dashes.ps1 Herein, we note the usefulness of using descriptive names for one's files... and/or functions. - - - # Copy Rename # Used to eliminate '.' and '-' from file name # REPLACEMENT LIST # ADD TO LIST AS APPROPRIATE $THIS_FOR_THAT = @(@(".","_"), @("-","_")) $DIR_IN = 'C:\alpha\input' $DIR_OUT = 'C:\alpha\output' $Items = Get-ChildItem -Path $DIR_IN ForEach ($old_name in $items) { $base = $old_name.BaseName $ext = $old_name.Extension #$base #$ext ForEach ($replacements in $THIS_FOR_THAT) { $new, $old = $replacements #$new, $old $base = $base.Replace($new, $old) } #$base #$ext $new_name = $base + $ext $path_in = JOIN-PATH $DIR_IN $old_name $path_out = JOIN-PATH $DIR_OUT $new_name $path_in $path_out Copy-Item $path_in $path_out } # # # # # # # # # # # # # # # # # # # # # # # # # # # # workspace.ps1 I like having a Scratch Pad file when working on code, so as to have a place to fire off one-liners and the like. - - - #Enter Test Picture # C:\alpha\gmic\gmic.exe input C:\alpha\input\test.jpg ` C:\alpha\gmic\gmic.exe ` C:\alpha\input\left.jpg ` C:\alpha\input\right.jpg ` append[0] [1],x ` remove[1] ` blur 2 C:\alpha\gmic\gmic.exe ` C:\alpha\input\left.jpg ` +mirror[0] x ` append[0] [1],x ` remove[1] ` output C:\alpha\output\both.jpg C:\alpha\gmic\gmic.exe ` C:\alpha\input\light.jpg ` +mirror[0] x ` append[0] [1],x ` remove[1] ` output C:\alpha\output\both.jpg # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # And there you go. Time Well Spent? or Time Spent? Is there much of a difference? #EOF