Search code examples
powershellcopypastesendkeyspowershell-cmdlet

How to copy and paste from a variable in PowerShell?


I'm trying to look up a value in PowerShell and paste it as a string into a line of text on another application.

In this instance I need to put the date into a line for an API call. I've found the date and can access the line where I need to input the date. I'm having trouble highlighting the date output, or using the variable where I have stored the date to paste that date into the line of code I'm using.

PowerShell code

$TDay = (Get-date).AddDays(-14)
Get-Date $TDay -Format MM-dd-yyyy

--API call

https:\\companyteam.teamwork.com/desk/v1/tickets/search.json?lastUpdated=07/26/2019

I'm able to delete the previous date but now I need to paste the value of the result from the PowerShell Script into where the date is displayed on the API call.

To solve my problem I think I'll need to manually highlight the results from the API call using the PowerShell mouse commands or if I could potentially just tell PowerShell to paste the result of Get-Date $TDay -Format MM-dd-yyyy


Solution

  • You can use Set-Clipboard to set the clipboard contents, which is the equivalent to a copy. You can use Get-Clipboard to paste copied contents to the console or as a variable value. You can also just paste from any copy-paste menu that has access to your clipboard.

    Get-Date $TDay -Format MM-dd-yyyy | Set-Clipboard
    
    $variable = Get-Clipboard
    

    Since I am not sure how you are making the API call, simplistically speaking let's assume the URI is just a string. You can still use the Get-Clipboard command or variable within the sub-expression operator $(). Variables don't technically need the sub-expression unless you are accessing properties of that variable.

    "https:\\companyteam.teamwork.com/desk/v1/tickets/search.json?lastUpdated=$(Get-Clipboard)"
    
    # OR 
    
    "https:\\companyteam.teamwork.com/desk/v1/tickets/search.json?lastUpdated=$variable"