I need to delete files matching some pattern (name containing a specific string) from a remote directory on an SFTP server using PS.
Delete files with pattern in remote SFTP directory
2k Views Asked by ITE At
2
There are 2 best solutions below
0
Mike Twc
On
You can also try ssh.net library (it's pretty lightweight)
https://github.com/sshnet/SSH.NET
After you build it the base syntax will look like this
Add-Type -Path "path\to\Renci.SshNet.dll"
$conn = New-Object Renci.SshNet.SftpClient -ArgumentList @($HostName, $PortNumber, $UserName, $Password)
$conn.connect()
$files = $conn.ListDirectory("DirName").FullName | where { $_ -like "*.csv"}
$files | foreach { $conn.Delete($_) }
You can also install GitBash, then you'll have ssh command available in your terminal.
Related Questions in POWERSHELL
- How to ignore warning errors?
- Data execution plan ended with error on DB restore
- Powershell Split a file name
- PowerShell EWS Save as for e-mails
- Run SQL Server Update Statement With Powershell
- using a global variable in multiple functions powershell
- Heisenberg was here: Aliases for PowerShell parameters only appear in cmdlet help when you do NOT document the cmdlet
- PowerShell Script to add newuser
- Why is PowerShell "not recognized" when installing Chocolatey?
- Enumerate a PSCustomObject as key/value pairs
- Unable to start program outside Windows folder
- Ask for creds only if some specified
- PowerShell 3 Parameters
- i can't ping a computer but remoting into it works (powershell enter-pssession)
- Feeding Variables in new-aduser -path option in powershell
Related Questions in SFTP
- Can I negatively close a SFTP file transfer from the client side?
- Does JSch allow to understand that I provided wrong credentials?
- Transfer File to Unix Server from Windows Shared Path using PuTTY
- How to use SFTP with PhpStorm to access server /var/www folder
- Transfer folder from one SFTP server to another every day
- Check if file exists using Apache Commons VFS2
- JSch Algorithm negotiation fail
- Apache SSHD: Where do I find the org/bouncycastle/crypto/prng/VMPCRandomGenerator class?
- Setting up a sftp server in node.js using ssh2 module
- Upload multiple photos to SFTP in Async
- Is this safe to share private key in JCraft JSch
- SFTP Processbuilder
- How to stop spring sftp inbound channel adapter polling when files are downloaded
- SFTP PHP 5.6 using phpseclib hangs
- Cannot create bean when start the application
Related Questions in REMOTE-SERVER
- To connect to Remote Desktop connection via local system
- Python Fabric - erasing my remote file when it should be copying to it
- How to register an installation remotely?
- curl php response 204 to download zip file
- Laravel remote server error 500, site doesn't works
- sshd@QNX: Could not load host key / Missing privileges separation
- Error:1722 Getting session names
- How do I issue a remote copy command?
- Stop SQL Triggers on Remote Server
- Netbeans extern connection
- XMPP remote server connection issue iOS
- Unable to establish SSL connection to MySQL server
- Can't connect to remote MySQL server on Windows
- How can I connect to a server using raw and sending string command to a server with C#
- COM+ activation on a remote server with partitions in C#
Related Questions in FILEPATTERN
- Delete files with pattern in remote SFTP directory
- What means tilde in windows file pattern
- How to set up SFTPSensor in Airflow to react on any file appearing on the server?
- Informatica Cloud (IICS) filename using wildcard
- Prettier - How to ignore certain file types in CLI file patterns?
- FFMPEG multiple file pattern not working in single command
- WildcardPatternSuite finds no classes
- Directory.GetFileSystemInfos(string) returns non-matching files
- How to set a variable in a Makefile to a filename via a file pattern, only if the pattern has ONE match
- Mule changing the file name/extension for storing multiple files after splitter
- Java recursively list the files from directory of specific pattern
- How do I loop over all Assets folders and sub folders and get to a list all the prefabs in this folders?
- PHP Glob function to find Controller files
- How do I get mocha to execute tests in all subfolders recursively?
- Recursively add files by pattern
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
There's no native support for SFTP in PowerShell. You have to use a 3rd party SFTP library.
For example with WinSCP .NET assembly, you can do this:
WinSCP GUI can generate code template for you.
(I'm the author of WinSCP)