Write a Powershell Script to Simplify SCP Transfer Syntax
-
Problem: the syntax for running scp in powershell is burdensome. Here's what it looks like for me:
PS C:\> scp -i C:\path\to\key\file -P <port> username@<static-ip-or-ddns-domain>:/path/to/destination/directory -
In powershell, run the following command. I did so after
cd .\Documents\. Name your script whatever you like.PS C:\> New-Item scp-transfer.ps1 -
Navigate to the file in File Explorer and open it with Notepad. Add the content below. The
paramenclosure defines command line arguments for us; that way, when we runscp-transfer.ps1 filename.ext, filename will be stored in$source.param( [string]$source ) scp -i C:\path\to\key\file -P <port> $source username@<static-ip-or-ddns-domain>:/path/to/destination/directory -
To make this script operational, we need to set the execution policy. Open Powershell with Run as Administrator, which can be done by right-clicking Powershell from the start menu. Run the following command. When it asks if your sure, enter `'Y'.
PS C:\> Set-ExecutionPolicy RemoteSigned -
Now you can easily transfer a file to your server with a much shorter command.
PS C:\> scp-transfer.ps1 filename.ext -
Find a directory where your scripts will live. Here's a recommendation:
C:\Users\User\powershell_scripts\ -
Let's make a Powershell profile if one doesn't already exist. Run
Test-Path $profile. If it returns false, we have to create one with the following.New-Item -path $profile -type file -force -
Navigate to and open the newly created file.
cd C:\Users\Admin\Documents\WindowsPowerShell\ notepad Microsoft.PowerShell_profile.ps1 -
Add the text below - an alias for our powershell script and Clear-Host.
new-item alias:scp-transfer -value C:\Users\User\powershell_scripts\scp-transfer.ps1 Clear-Host -
Done! Now you can run
scp-transfer filename.extwithout worrying about the absolute path to the script.





