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;
}







1















I need to delete files matching some pattern (name containing a specific string) from a remote directory on an SFTP server using PS.










share|improve this question




















  • 1





    Please read about how to ask a good question.

    – mklement0
    Nov 16 '18 at 15:12


















1















I need to delete files matching some pattern (name containing a specific string) from a remote directory on an SFTP server using PS.










share|improve this question




















  • 1





    Please read about how to ask a good question.

    – mklement0
    Nov 16 '18 at 15:12














1












1








1


1






I need to delete files matching some pattern (name containing a specific string) from a remote directory on an SFTP server using PS.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















2














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)






share|improve this answer

































    0














    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.






    share|improve this answer





















    • 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














    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    2














    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)






    share|improve this answer






























      2














      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)






      share|improve this answer




























        2












        2








        2







        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)






        share|improve this answer















        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)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 15:24

























        answered Nov 16 '18 at 15:12









        Martin PrikrylMartin Prikryl

        91.8k22183386




        91.8k22183386

























            0














            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.






            share|improve this answer





















            • 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


















            0














            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.






            share|improve this answer





















            • 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
















            0












            0








            0







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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
















            • 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




















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine