Delete files with pattern in remote SFTP directory
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I need to delete files matching some pattern (name containing a specific string) from a remote directory on an SFTP server using PS.
powershell sftp remote-server filepattern
add a comment |
I need to delete files matching some pattern (name containing a specific string) from a remote directory on an SFTP server using PS.
powershell sftp remote-server filepattern
1
Please read about how to ask a good question.
– mklement0
Nov 16 '18 at 15:12
add a comment |
I need to delete files matching some pattern (name containing a specific string) from a remote directory on an SFTP server using PS.
powershell sftp remote-server filepattern
I need to delete files matching some pattern (name containing a specific string) from a remote directory on an SFTP server using PS.
powershell sftp remote-server filepattern
powershell sftp remote-server filepattern
edited Nov 16 '18 at 17:19
Ansgar Wiechers
146k13135193
146k13135193
asked Nov 16 '18 at 14:41
ITEITE
223
223
1
Please read about how to ask a good question.
– mklement0
Nov 16 '18 at 15:12
add a comment |
1
Please read about how to ask a good question.
– mklement0
Nov 16 '18 at 15:12
1
1
Please read about how to ask a good question.
– mklement0
Nov 16 '18 at 15:12
Please read about how to ask a good question.
– mklement0
Nov 16 '18 at 15:12
add a comment |
2 Answers
2
active
oldest
votes
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:
Add-Type -Path "WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "example.com"
UserName = "username"
Password = "password"
SshHostKeyFingerprint = "ssh-rsa 2048 ..."
}
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$session.RemoveFiles("/remote/path/*string*").Check()
$session.Dispose()
WinSCP GUI can generate code template for you.
(I'm the author of WinSCP)
add a comment |
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 "pathtoRenci.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.
1
Updated my response. There is no native support of wildcards, but this can achieved the same easily with where
– Mike Twc
Nov 16 '18 at 18:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339986%2fdelete-files-with-pattern-in-remote-sftp-directory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
Add-Type -Path "WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "example.com"
UserName = "username"
Password = "password"
SshHostKeyFingerprint = "ssh-rsa 2048 ..."
}
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$session.RemoveFiles("/remote/path/*string*").Check()
$session.Dispose()
WinSCP GUI can generate code template for you.
(I'm the author of WinSCP)
add a comment |
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:
Add-Type -Path "WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "example.com"
UserName = "username"
Password = "password"
SshHostKeyFingerprint = "ssh-rsa 2048 ..."
}
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$session.RemoveFiles("/remote/path/*string*").Check()
$session.Dispose()
WinSCP GUI can generate code template for you.
(I'm the author of WinSCP)
add a comment |
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:
Add-Type -Path "WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "example.com"
UserName = "username"
Password = "password"
SshHostKeyFingerprint = "ssh-rsa 2048 ..."
}
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$session.RemoveFiles("/remote/path/*string*").Check()
$session.Dispose()
WinSCP GUI can generate code template for you.
(I'm the author of WinSCP)
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:
Add-Type -Path "WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "example.com"
UserName = "username"
Password = "password"
SshHostKeyFingerprint = "ssh-rsa 2048 ..."
}
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$session.RemoveFiles("/remote/path/*string*").Check()
$session.Dispose()
WinSCP GUI can generate code template for you.
(I'm the author of WinSCP)
edited Nov 16 '18 at 15:24
answered Nov 16 '18 at 15:12
Martin PrikrylMartin Prikryl
91.8k22183386
91.8k22183386
add a comment |
add a comment |
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 "pathtoRenci.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.
1
Updated my response. There is no native support of wildcards, but this can achieved the same easily with where
– Mike Twc
Nov 16 '18 at 18:05
add a comment |
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 "pathtoRenci.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.
1
Updated my response. There is no native support of wildcards, but this can achieved the same easily with where
– Mike Twc
Nov 16 '18 at 18:05
add a comment |
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 "pathtoRenci.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.
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 "pathtoRenci.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.
edited Nov 16 '18 at 18:03
answered Nov 16 '18 at 16:45
Mike TwcMike Twc
1,2361713
1,2361713
1
Updated my response. There is no native support of wildcards, but this can achieved the same easily with where
– Mike Twc
Nov 16 '18 at 18:05
add a comment |
1
Updated my response. There is no native support of wildcards, but this can achieved the same easily with where
– Mike Twc
Nov 16 '18 at 18:05
1
1
Updated my response. There is no native support of wildcards, but this can achieved the same easily with where
– Mike Twc
Nov 16 '18 at 18:05
Updated my response. There is no native support of wildcards, but this can achieved the same easily with where
– Mike Twc
Nov 16 '18 at 18:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339986%2fdelete-files-with-pattern-in-remote-sftp-directory%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please read about how to ask a good question.
– mklement0
Nov 16 '18 at 15:12