Image Verification - Argument Passed to Script

I have this simple Script listed below:
$FileFromClipboard = Get-Clipboard -Format Image
$FileFromClipBoard.Save(“C:\TEMP\Image.jpg”)

How would I

  1. Verify that an Image actually exists in the Clipboard prior to executing the actual Commands?
  2. Is it possible to pass an Argument to this Script? I am referring to the Path of the Image File which I would like to be dynamic, something similar to a Replaceable Parameter in a Batch File?

NOTE: Thanks in advance, I am still getting used to this Formatting, so please excuse my clumsiness. Do you have any Tutorial or Guidelines relating to this?

  1. You can create the variable and check if its null. If it is, there is no image in the clipboard.
$FileFromClipboard = Get-Clipboard -Format Image
If ($null -eq $FileFromClipboard) {
    Write-Host 'There is no image in the clipboard'
}
Else {
    Write-Host 'There is an image in the clipboard'
}
  1. You were not very specific regarding the file name/path format you want but here is an example which I think aligns to what you are looking for. If not, let me know, perhaps I did not correctly understand your requirement.
$Filename = "Image_$(Get-Date -Format 'MM.dd.yyyy').jpg"
$FileFromClipBoard.Save("C:\TEMP\$Filename")

First off, regarding code formatting, follow this tutorial:

If there’s no image on the clipboard then $FileFromClipboard will be NULL so you can just check the result of the Get-Clipboard command.

$FileFromClipboard = Get-Clipboard -Format Image
if ($FileFromClipboard -ne $null) {
    $FileFromClipBoard.Save('C:\Temp\Image.png')
}
else {
    Write-Warning 'No image on clipboard.'
}

You have options for the path. Whichever way you choose, you’re replacing the path with a variable. The simplest way is with Read-Host.

$path = Read-Host 'Enter the path to save the image'
$FileFromClipboard = Get-Clipboard -Format Image
if ($FileFromClipboard -ne $null) {
    $FileFromClipBoard.Save($path)
}
else {
    Write-Warning 'No image on clipboard.'
}

The more proper way is to use parameters but this is a bit more work for a beginner. Have a look at the help for Get-Help about_functions_advanced for a guide to these.

Thank you ferc for your Reply and I apologize for not being specific. What I was referring to was if there was any Method of Passing an Argument to a PS Script, analogous to passing an Argument to a Function. Pseudo Code would be:

Execute CopyImageInClipboard.ps1(“C:\Demo\Image.jpg”) - The Script would now be executed and the Image in the Clipboard would be saved to the C:\Demo Folder as Image.jpg.

Thanks matt-bloomfeld for the advice. For years I have been used to programming in Visual Basic/VBA, and I guess I need to get into a different mindset to learn PowerShell. As far as the Tutorial, I will definitely take it.

It seems you are looking for a parameterized script. You need to add ‘Path’ as a parameter within the parameter block and then run the script from the command line. Then you can add the image full path as parameter value / argument.

Param (
    [string]$Path
)

BEGIN {
    $FileFromClipboard = Get-Clipboard -Format Image
}

PROCESS {

    If ($null -eq $FileFromClipboard) {
        Write-Host 'There is no image in the clipboard'
        Exit
    }
    Else {
        Write-Host 'Image saved'
        $FileFromClipBoard.Save($Path)
    }

}

Assuming you saved your script to the ‘C:\Scripts’ folder, and you want to save the image to ‘C:\Temp’ directory, with the file name ‘Image.jpg’, you would have to run this:

C:\Scripts\SaveImageFromClipboard.ps1 -Path C:\Temp\Image.jpg

This is exactly what I was looking for. I will run the Revised Script and Call it passing the Path Argument. Thanks again for all your help.

@ferc:
The Parameterized Script Code worked perfectly. I am now able to execute a PowerShell Script from an External Process, namely the Click() Event of a Command Button on an Access Form. More importantly, I can pass to the PS Script an Argument specifying the Path to Save the Clipboard Image in. Thanks again for all your help.

NOTE: Aside from the Script Code, I don’t know if you want me to Post the Code from the External Process that actually Executes the Script, since it is in VBA, and is not relevant to PowerShell, except in this context. If you think otherwise, I will gladly Post it should others need to reference this approach.

If you’re willing to share your sollution you may consider creating a public GitHub repository for it. That’s easier to share and it even enables others to contribute if they have some ideas for improvement.

I would definitely be interested to see it, I used to do a lot of Excel VBA coding. As Olaf suggested, if you have a GitHub repository and can share the code there that would be great. You can also use GitHub Gist if you find it more convenient. That is really up to you. :slight_smile:

I have never used GitHub, and obviously have no idea how to create a Repository within it. I belong to a Software Development Forum where we deal almost exclusively with Excel, Access, and SQL Server. Some Topics addressed are frequently VBA, Automation Code (Excel, Access, Outlook), Recordset Programming, Access GUI Issues, Relational Database Management, Table Structures, etc.