{"componentChunkName":"component---src-templates-articles-index-jsx","path":"/articles/manage-files-over-sftp-powershell/","result":{"data":{"props":{"adsense":true,"adsenseMobile":null,"byLine":"","canonical":"https://staging.business.com/articles/manage-files-over-sftp-powershell/","content":"<p>Although native support for Secure File Transfer Protocol (SFTP) is not in PowerShell, by using a free module, we can retrieve, delete and add new files.</p>\n<p><picture class=\"aligncenter full\">The module we need to use is called Posh-SSH. This module is available on the PowerShell Gallery and can be installed by running Install-Module -Name Posh-SSH. Once installed, we can take a look at all of the available commands it now gives us:</picture></p>\r\n<p>PS C:&gt; Get-Command -Module posh-ssh -Noun *SFTP*<br><br>CommandType     Name                                     Version     Source<br>-----------              ----                                       -------      ------<br>Function              Get-SFTPChildItem                    2.0.1      posh-ssh<br>Function              Get-SFTPContent                       2.0.1      posh-ssh<br>Function              Get-SFTPLocation                      2.0.1      posh-ssh<br>Function              Get-SFTPPathAttribute               2.0.1      posh-ssh<br>Function             Get-SFTPSession                        2.0.1      posh-ssh<br>Function              New-SFTPFileStream                 2.0.1      posh-ssh<br>Function              New-SFTPItem                          2.0.1      posh-ssh<br>Function              New-SFTPSymlink                      2.0.1     posh-ssh<br>Function              Remove-SFTPItem                    2.0.1      posh-ssh<br>Function              Remove-SFTPSession                2.0.1      posh-ssh<br>Function             Rename-SFTPFile                       2.0.1      posh-ssh<br>Function             Set-SFTPContent                       2.0.1       posh-ssh<br>Function             Set-SFTPLocation                       2.0.1      posh-ssh<br>Function             Set-SFTPPathAttribute                2.0.1      posh-ssh<br>Function              Test-SFTPPath                          2.0.1      posh-ssh<br>Cmdlet                Get-SFTPFile                             2.0.1      posh-ssh<br>Cmdlet               New-SFTPSession                       2.0.1      posh-ssh<br>Cmdlet               Set-SFTPFile                              2.0.1      posh-ssh</p>\r\n<p>Right away, it looks like can do a lot with this module. Let's dig into some of the SFTP commands and see how they can be used.</p>\r\n<h2>Options for the model</h2>\r\n<p>To transfer files over SFTP, we need to establish a session to the SFTP server. We'll only have to do this once. We could create a separate session each time we need to perform some task over SFTP, but that wouldn't be too efficient.</p>\r\n<p>Instead, let's create a single SFTP session using the New-SFTPSession command. We're going to take the easy route and not mess with certificates so we can use a username and password to authenticate to the SFTP server. The New-SFTPSession has a Credential parameter that accepts a PSCredential object. We'll use the Get-Credential command to prompt the user for a username and password.</p>\r\n<p>$credential = Get-Credential</p>\r\n<p>Once we've got the username and password capture, we can then pass that to the New-SFTPSession command along with the AcceptKey parameter. The AcceptKey parameter will automatically accept the key that's returned from the SFTP server rather than prompting you to do so.</p>\r\n<p>$session = New-SFTPSession -ComputerName 'MYSFTPSERVER' -Credential $Credential -AcceptKey</p>\r\n<p>If all goes well, you'll be returned back to the console. If so, we can now use this session with a number of commands. For example, if I need to download a file from the SFTP server to my local computer, I can use the Get-SFTPFile function.</p>\r\n<p>$getParams = @{<br>    SessionId = $session.SessionId<br>    LocalPath = 'C:localfile.txt'<br>   RemoteFile = 'C:localfile.txt'<br>}<br>Get-SFTPFile @getParams</p>\r\n<p>Perhaps we need to remove a file on the SFTP server. We can use the Remove-SFTPItem function for that just about as easily as we use the Get-SFTPFile function.</p>\r\n<p>Get-SFTPFile -SessionId $session.SessionId -Path 'C:localfile.txt'</p>\r\n<p>Once we've done whatever we need to do on the SFTP server, we should then disconnect and remove the session. We could just call Remove-SFTPSession and provide the session but it's always better to check ahead of time just to be sure the session is still there. We can check to see if the session is still created by using the Get-SFTPSession.</p>\r\n<h2>Summary</h2>\r\n<p>You can see below that I'm first checking to see if our session exists. If so, I'm then disconnecting it and completely removing it from our session.</p>\r\n<p>if ($session = Get-SFTPSession -SessionId $session.SessionId) {<br>    $session.Disconnect()<br>}<br>$null = Remove-SftpSession -SftpSession $session</p>\r\n<p>The next time you need to perform any kind of SFTP task, have a look at the Posh-SSH PowerShell module.</p>\n","docType":"article","guid":"articles_016f107a51d0dab95d30bc62b560bc6e","lastModified":"2018-09-05T07:00:00.000Z","published":"2018-09-05T07:00:00.000Z","structuredData":null,"subtitle":"Native support for Secure File Transfer Protocol (SFTP) is not in PowerShell.","template":"v1","title":"Managing Files over SFTP with PowerShell","type":"article","author":{"avatar":null,"bio":"Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.","name":"Adam Bertram","image":null,"slug":null,"social":{"facebook":"","linkedin":"https://www.linkedin.com/in/adambertram/","twitter":"https://twitter.com/adbertram","website":"https://www.adamtheautomator.com/"},"status":"enabled","title":null,"type":"Contributing Writer","user":{"imagePath":"https://staging.business.com/images/undefined","profile":{"company":"eGain Communications","jobTitle":"Product Management Analyst","website":""}}},"breadcrumbs":null,"channels":{"primary":{"name":"Technology","slug":"technology"},"sub":{"name":"Technology Solutions","slug":"technology-solutions"}},"meta":{"robots":"index, follow","description":"Although native support for Secure File Transfer Protocol (SFTP) is not in PowerShell, by using a free module, we can retrieve, delete and add new files.","title":"Managing Files over SFTP with PowerShell - business.com"},"parsedContent":{"toc":[{"id":"options-for-the-model","title":"Options for the model"},{"id":"summary","title":"Summary"}],"json":[{"component":"p","children":["Although native support for Secure File Transfer Protocol (SFTP) is not in PowerShell, by using a free module, we can retrieve, delete and add new files."]},{"component":"p","children":[{"component":"picture","props":{"className":"aligncenter full"},"children":["The module we need to use is called Posh-SSH. This module is available on the PowerShell Gallery and can be installed by running Install-Module -Name Posh-SSH. Once installed, we can take a look at all of the available commands it now gives us:"]}]},{"component":"p","children":["PS C:> Get-Command -Module posh-ssh -Noun *SFTP*",{"component":"br"},{"component":"br"},"CommandType     Name                                     Version     Source",{"component":"br"},"-----------              ----                                       -------      ------",{"component":"br"},"Function              Get-SFTPChildItem                    2.0.1      posh-ssh",{"component":"br"},"Function              Get-SFTPContent                       2.0.1      posh-ssh",{"component":"br"},"Function              Get-SFTPLocation                      2.0.1      posh-ssh",{"component":"br"},"Function              Get-SFTPPathAttribute               2.0.1      posh-ssh",{"component":"br"},"Function             Get-SFTPSession                        2.0.1      posh-ssh",{"component":"br"},"Function              New-SFTPFileStream                 2.0.1      posh-ssh",{"component":"br"},"Function              New-SFTPItem                          2.0.1      posh-ssh",{"component":"br"},"Function              New-SFTPSymlink                      2.0.1     posh-ssh",{"component":"br"},"Function              Remove-SFTPItem                    2.0.1      posh-ssh",{"component":"br"},"Function              Remove-SFTPSession                2.0.1      posh-ssh",{"component":"br"},"Function             Rename-SFTPFile                       2.0.1      posh-ssh",{"component":"br"},"Function             Set-SFTPContent                       2.0.1       posh-ssh",{"component":"br"},"Function             Set-SFTPLocation                       2.0.1      posh-ssh",{"component":"br"},"Function             Set-SFTPPathAttribute                2.0.1      posh-ssh",{"component":"br"},"Function              Test-SFTPPath                          2.0.1      posh-ssh",{"component":"br"},"Cmdlet                Get-SFTPFile                             2.0.1      posh-ssh",{"component":"br"},"Cmdlet               New-SFTPSession                       2.0.1      posh-ssh",{"component":"br"},"Cmdlet               Set-SFTPFile                              2.0.1      posh-ssh"]},{"component":"p","children":["Right away, it looks like can do a lot with this module. Let's dig into some of the SFTP commands and see how they can be used."]},{"component":"h2","props":{"id":"options-for-the-model"},"children":["Options for the model"]},{"component":"p","children":["To transfer files over SFTP, we need to establish a session to the SFTP server. We'll only have to do this once. We could create a separate session each time we need to perform some task over SFTP, but that wouldn't be too efficient."]},{"component":"p","children":["Instead, let's create a single SFTP session using the New-SFTPSession command. We're going to take the easy route and not mess with certificates so we can use a username and password to authenticate to the SFTP server. The New-SFTPSession has a Credential parameter that accepts a PSCredential object. We'll use the Get-Credential command to prompt the user for a username and password."]},{"component":"p","children":["$credential = Get-Credential"]},{"component":"p","children":["Once we've got the username and password capture, we can then pass that to the New-SFTPSession command along with the AcceptKey parameter. The AcceptKey parameter will automatically accept the key that's returned from the SFTP server rather than prompting you to do so."]},{"component":"p","children":["$session = New-SFTPSession -ComputerName 'MYSFTPSERVER' -Credential $Credential -AcceptKey"]},{"component":"p","children":["If all goes well, you'll be returned back to the console. If so, we can now use this session with a number of commands. For example, if I need to download a file from the SFTP server to my local computer, I can use the Get-SFTPFile function."]},{"component":"p","children":["$getParams = @{",{"component":"br"},"    SessionId = $session.SessionId",{"component":"br"},"    LocalPath = 'C:localfile.txt'",{"component":"br"},"   RemoteFile = 'C:localfile.txt'",{"component":"br"},"}",{"component":"br"},"Get-SFTPFile @getParams"]},{"component":"p","children":["Perhaps we need to remove a file on the SFTP server. We can use the Remove-SFTPItem function for that just about as easily as we use the Get-SFTPFile function."]},{"component":"p","children":["Get-SFTPFile -SessionId $session.SessionId -Path 'C:localfile.txt'"]},{"component":"p","children":["Once we've done whatever we need to do on the SFTP server, we should then disconnect and remove the session. We could just call Remove-SFTPSession and provide the session but it's always better to check ahead of time just to be sure the session is still there. We can check to see if the session is still created by using the Get-SFTPSession."]},{"component":"h2","props":{"id":"summary"},"children":["Summary"]},{"component":"p","children":["You can see below that I'm first checking to see if our session exists. If so, I'm then disconnecting it and completely removing it from our session."]},{"component":"p","children":["if ($session = Get-SFTPSession -SessionId $session.SessionId) {",{"component":"br"},"    $session.Disconnect()",{"component":"br"},"}",{"component":"br"},"$null = Remove-SftpSession -SftpSession $session"]},{"component":"p","children":["The next time you need to perform any kind of SFTP task, have a look at the Posh-SSH PowerShell module."]}]},"resource":null,"sponsorship":null,"thumbnail":{"path":"https://staging.business.com/images/content/5d0/1c54e9c658ac27e8b4571/1500-0-","caption":"kikujungboy/Shutterstock","isBlurred":false},"bestPicks":null},"related":{"relatedArticles":[{"title":"PowerShell: Everything You Need to Know","canonical":"https://www.business.com/articles/powershell-everything-you-need-to-know/","slug":"powershell-everything-you-need-to-know","thumbnail":"https://www.business.com/images/content/5b8/998581039eb8f738b457b/1500-0-","dek":"What you need to know to unlock Microsoft PowerShell's full capabilities.","lastModified":"2018-09-05T17:00:00Z","published":"2018-09-05T17:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Managing Active Directory","canonical":"https://www.business.com/articles/powershell-active-directory-cmdlets/","slug":"powershell-active-directory-cmdlets","thumbnail":"https://www.business.com/images/content/5cf/90cab7b4374ab068b456f/1500-0-","dek":"Administering Active Directory is one of the most critical roles in any Windows network.","lastModified":"2018-09-04T08:00:00Z","published":"2018-09-04T08:00:00Z","sponsored":false},{"title":"Use PowerShell to Make WSUS Suck Less","canonical":"https://www.business.com/articles/powershell-windows-software-update-services/","slug":"powershell-windows-software-update-services","thumbnail":"https://www.business.com/images/content/5b8/eca2d92a3a4ee5a8b4574/1500-0-","dek":"Windows Software Update Services can use PowerShell to work with servers and Windows.","lastModified":"2018-09-04T10:00:00Z","published":"2018-09-04T10:00:00Z","sponsored":false},{"title":"Essential PowerShell Cmdlets For Auditing And Maintaining Storage","canonical":"https://www.business.com/articles/powershell-for-storage/","slug":"powershell-for-storage","thumbnail":"https://www.business.com/images/content/5d0/18e065a215ea2498b4588/1500-0-","dek":"PowerShell v5 offers some new features for managing storage.","lastModified":"2018-09-04T11:00:00Z","published":"2018-09-04T11:00:00Z","sponsored":false},{"title":"How To Manage IIS Websites With PowerShell","canonical":"https://www.business.com/articles/powershell-manage-iis-websites/","slug":"powershell-manage-iis-websites","thumbnail":"https://www.business.com/images/content/5d0/07b2b7b437409688b4568/1500-0-","dek":"When setting up a new website in IIS are you still using the IIS Web Manager?","lastModified":"2018-09-06T07:00:00Z","published":"2018-09-06T07:00:00Z","sponsored":false},{"title":"How To Manage User Profiles With PowerShell","canonical":"https://www.business.com/articles/powershell-manage-user-profiles/","slug":"powershell-manage-user-profiles","thumbnail":"https://www.business.com/images/content/5cf/ffdf07b4374ac348b4576/1500-0-","dek":"Managing Windows user profiles can be time consuming and tedious. Here are some tips.","lastModified":"2018-09-06T07:00:00Z","published":"2018-09-06T07:00:00Z","sponsored":false},{"title":"Mobile Wallet Guide: Google Pay vs. Apple Pay vs. Samsung Pay - 08-05","canonical":"https://staging.business.com/articles/google-pay-vs-apple-pay-vs-samsung-pay/","slug":"google-pay-vs-apple-pay-vs-samsung-pay","thumbnail":"https://staging.business.com/images/content/5c5/afd7f54d26d67178b458d/1500-0-","dek":"Edit 07-24 - subtitle","lastModified":"2019-08-05T20:58:00Z","published":"2018-06-21T16:05:00Z","sponsored":null},{"title":"Mobile Wallet Guide: Google Pay vs. Apple Pay vs. Samsung Pay","canonical":"https://www.business.com/articles/google-pay-vs-apple-pay-vs-samsung-pay/","slug":"google-pay-vs-apple-pay-vs-samsung-pay","thumbnail":"https://www.business.com/images/content/5c5/e0a269c658a457c8b4591/1500-0-","dek":"You no longer need to carry a bulky wallet filled with numerous credit cards or cash.","lastModified":"2018-06-21T16:06:00Z","published":"2018-06-21T16:05:00Z","sponsored":false},{"title":"How To Manage IIS Application Pools With PowerShell","canonical":"https://www.business.com/articles/powershell-manage-iis-application-pools/","slug":"powershell-manage-iis-application-pools","thumbnail":"https://www.business.com/images/content/5d0/0067d5a215edb1a8b4585/1500-0-","dek":"Where you turn when you're managing an IIS web server. farm with potentially dozens or h","lastModified":"2018-09-06T08:00:00Z","published":"2018-09-06T08:00:00Z","sponsored":false},{"title":"Introduction To Using XML Files With PowerShell","canonical":"https://www.business.com/articles/powershell-read-xml-files/","slug":"powershell-read-xml-files","thumbnail":"https://www.business.com/images/content/5db/f21da9c658a05188b4577/1500-0-","dek":"PowerShell offers several ways to read XML documents, without writing a lot of code.","lastModified":"2018-09-05T12:01:00Z","published":"2018-09-05T12:00:00Z","sponsored":false},{"title":"How To Resolve Active Directory Account Lockouts With PowerShell","canonical":"https://www.business.com/articles/powershell-active-directory-lockouts/","slug":"powershell-active-directory-lockouts","thumbnail":"https://www.business.com/images/content/5cf/894749c658a6a5a8b457e/1500-0-","dek":"How do you track and resolve a locked Active Directory account? Find out.","lastModified":"2018-09-05T11:00:00Z","published":"2018-09-05T11:00:00Z","sponsored":false},{"title":"Your Guide to Creating a Small Business Marketing Plan","canonical":"https://www.business.com/articles/sample-marketing-plan-outline-and-template/","slug":"sample-marketing-plan-outline-and-template","thumbnail":"https://www.business.com/images/content/5f3/3e1765a215eb62d8b4567/1500-0-","dek":"Follow these templates and guidelines to get started on creating your business's marketing plan.","lastModified":"2020-08-12T12:28:00Z","published":"2018-03-02T13:00:00Z","sponsored":false}],"relatedReviews":[{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df908cc5a215e8c4a8b4568/conferencecall_Antonio_Diaz_Getty.jpg","canonical":"https://staging.business.com/categories/best-conference-call-services/","title":"The Best Conference Call Services of 2021","dek":"Which conference call service is right for your business in 2021? We review the top-rated services.","slug":"best-conference-call-services","lastModified":"2021-03-17T18:17:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5df3d8695a215e58428b4570/POS_GETTY_DERO2084.jpg","canonical":"https://staging.business.com/categories/best-pos-systems/","title":"The Best POS Systems of 2021","dek":"Before you choose a POS system, check out our reviews of the best picks for 2021. ","slug":"best-pos-systems","lastModified":"2021-03-17T17:33:00Z"},{"doc_type":"category","thumbnail":"https://staging.business.com/images/rz/5e00d4787b43745d6a8b456e/Laptop_undrey_getty.jpg","canonical":"https://staging.business.com/categories/employee-monitoring-software/","title":"The Best Employee Monitoring Software of 2021","dek":"We've narrowed down the best employee monitoring software for businesses. Check out our best picks and reviews.","slug":"employee-monitoring-software","lastModified":"2021-03-05T15:40:00Z"}],"relatedSponsoredArticles":null}},"pageContext":{"displayModified":"2018-09-05T07:00:00.000Z","template":"v1","title":"Managing Files over SFTP with PowerShell","meta":{"robots":"index, follow"},"docType":"articles","slug":"manage-files-over-sftp-powershell","canonical":"https://staging.business.com/articles/manage-files-over-sftp-powershell/","skipTOC":false}},"staticQueryHashes":[]}