none
Delete specific files starting with "X" and ending with "Y" Sharepoint online- powershell RRS feed

  • Question

  • Hello

    I have a task to delete files starting with specific latter and ending with ( for examp. "old"). The reason behind in is to remove old files from the month and keep only one.For the current month only the last file. For every previous month, only the last file of that month needs to remain.So far I got this but it is gaving me only error.And I do not have searching part to find only needed files. I will be very happy if someone can help me out. It is urgent one. Thanks

    Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet,

    $web = Get-SPWeb "site"
    $Libraries = $web.Lists | where {$_.BaseType -eq "Documents"}
     
    foreach ($library in $Libraries) {
     
        Write-Output "Getting files from $($library.Title)"
        $Files = $library.Items | where {$_.FileSystemObjectType -eq "File"}
        foreach ($file in $Files) {
            Write-Output "Deleting file $($file.Name)..."
            $file.Delete()
        }
    }

    Thursday, August 22, 2019 5:41 AM

Answers

  • As you have discovered, guesswork is not an effective problem-solving approach (particularly with programming).

    Your questions are essentially out of scope for this forum because you're asking others to debug/rewrite/fix code for your specific situation. Please read this first (it is the very first post from right at the top of this forum):

    This forum is for scripting questions rather than script requests

    Your question is not a specific scripting question but rather a vague "here's some code that doesn't work, I don't know why, can someone please fix/debug/rewrite it for me" question. That's not a scripting question; that's a request for free consulting work. That's not the purpose of this forum.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Tuesday, August 27, 2019 5:53 PM
    • Marked as answer by Bill_StewartModerator Tuesday, January 21, 2020 3:39 PM
    Monday, August 26, 2019 3:12 PM
    Moderator

All replies

  • $PathToWatch = "C:\temp"
    $FiletoKeep = Get-ChildItem -Path $PathToWatch -Recurse -include begin*, *.old | sort LastWritetime | Select-Object -First 1
    $FilesToDelete = Get-ChildItem -Path $PathToWatch -Recurse -include begin*, *.old -Exclude $FiletoKeep
    
    foreach ($file in $FilesToDelete)
        {
        Remove-Item -Path $file.fullName 
        Write-Output "$($file.Name) has been removed"
        }

    Perhaps something like this.

    Olivier


    Thursday, August 22, 2019 6:23 AM
  • Thank you a lot,  Olivier,

    I think

    $PathToWatch = "C:\temp" 

    It is pointing to a local path, I need to connect to Sharepoint online , give the right Document library and specify the name or in

    $PathToWatch = "C:\temp" to give a csv file in there the URL of the site?

    How do you suggest to do it?
    • Edited by Verelyn Thursday, August 22, 2019 6:41 AM
    Thursday, August 22, 2019 6:41 AM
  • Please post SharePoint specific issues in the correct SharePoint forum.


    \_(ツ)_/

    Thursday, August 22, 2019 7:32 AM
  • Not an issue, just asking for advice how to do the script. The error that I got, it is not the main in the question. But thanks anyway, I am new in the forum so next time when I have i will know ;)
    Thursday, August 22, 2019 11:11 AM
  • Hi Verelyn,

    I've found this : powershell-get-a-list-of-all-the-document-libraries-for-a-web-application-incl

    Look at the answer, I haven't any Sharepoint to test this but it seems to give a .csv file with all documents in the library. Just treat the .csv file as I did it in my answer.

    $PathToWatch = Import-Csv -Path c:\temp\LibFiles.csv

    regards.

    Olivier

    P.S. : don't forget to mark the question as resolve.


    Thursday, August 22, 2019 1:21 PM
  • Thanks a lot

    I saw it but it I can not find a way to use it about deleting by name.

    Thursday, August 22, 2019 5:04 PM
  • When  I run this

    $PathToWatch = Import-Csv -Path .\folders.txt -header 'location','prefix' -Delimiter ";"

    $FiletoKeep = Get-ChildItem -Path $PathToWatch -Recurse -include begin*BTS*.old | sort LastWritetime | Select-Object -First 1
    $FilesToDelete = Get-ChildItem -Path $PathToWatch -Recurse -include begin*BTS*.old -Exclude $FiletoKeep

    foreach ($file in $FilesToDelete)
        {
        Remove-Item -Path $file.fullName
        Write-Output "$($file.Name) has been removed"
        }

    I got this

    Get-ChildItem : Cannot find drive. A drive with the name '@{location=https' does not exist.
    At line:3 char:15
    + ... iletoKeep = Get-ChildItem -Path $PathToWatch -Recurse -include begin* ...

    Thursday, August 22, 2019 5:22 PM
  • -Exclude $FiletoKeep.Name

    \_(ツ)_/

    Thursday, August 22, 2019 6:42 PM
  • Hi

    I added

    $siteurl = "https..."
    $Libraries = $siteurl.Lists | where {$_.BaseType -eq "Documents"}
    Connect-PnPOnline -Url $siteurl  -UseWebLogin

    $FiletoKeep = Get-ChildItem -Path $PathToWatch -Recurse -include begin*BTS*.OLD | sort LastWritetime | Select-Object -First 1
    $FilesToDelete = Get-ChildItem -Path $PathToWatch -Recurse -include begin *BTS* -Exclude $FiletoKeep.Name

    foreach ($file in $FilesToDelete)
        {
        Remove-Item -Path $file.fullName
        Write-Output "$($file.Name) has been removed"
        }

    And I am getting the same error

    Get-ChildItem : Cannot find drive. A drive with the name '@{location=https' does not exist.
    At line:6 char:15
    + ... iletoKeep = Get-ChildItem -Path $PathToWatch -Recurse -include begin* ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Friday, August 23, 2019 6:00 PM
  • Any idea

    I was google it a lot and tried different things but no result at all.

    Saturday, August 24, 2019 11:57 AM
  • As you have discovered, guesswork is not an effective problem-solving approach (particularly with programming).

    Your questions are essentially out of scope for this forum because you're asking others to debug/rewrite/fix code for your specific situation. Please read this first (it is the very first post from right at the top of this forum):

    This forum is for scripting questions rather than script requests

    Your question is not a specific scripting question but rather a vague "here's some code that doesn't work, I don't know why, can someone please fix/debug/rewrite it for me" question. That's not a scripting question; that's a request for free consulting work. That's not the purpose of this forum.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Tuesday, August 27, 2019 5:53 PM
    • Marked as answer by Bill_StewartModerator Tuesday, January 21, 2020 3:39 PM
    Monday, August 26, 2019 3:12 PM
    Moderator
  • Hello Bill

    I did not request someone to do a script for me! I shared mine and please ask if someone can help...meaning someone with more experience that me can help. I tried everything before posting! The code is working but the SPweb is only for on-premise used. And I found that myself!!! Where exactly I said " do the script for me or this one is not working I do not way or request for a new script" Why dont you check all forums requests and see what other people are posting?! I am very thankful to the people who answered my question. " free consulting work ???? " just cuz I was asking a question ?????? It is a scripting forum I DID NOT request a script just posting mine!! I will never use this *** site again and will escalate this one, dear moderator Bill.

    https://social.technet.microsoft.com/Forums/windows/en-US/e8629d4a-8d06-4f33-9cfa-e055d48371d7/retrieve-errors-from-excel-file-using-powershell?forum=winserverpowershell

    Tuesday, August 27, 2019 11:47 AM
  • Hello Bill

    I did not request someone to do a script for me! I shared mine and please ask if someone can help...meaning someone with more experience that me can help. I tried everything before posting! The code is working but the SPweb is only for on-premise used. And I found that myself!!! Where exactly I said " do the script for me or this one is not working I do not way or request for a new script" Why dont you check all forums requests and see what other people are posting?! I am very thankful to the people who answered my question. " free consulting work ???? " just cuz I was asking a question ?????? It is a scripting forum I DID NOT request a script just posting mine!! I will never use this *** site again and will escalate this one, dear moderator Bill.

    https://social.technet.microsoft.com/Forums/windows/en-US/e8629d4a-8d06-4f33-9cfa-e055d48371d7/retrieve-errors-from-excel-file-using-powershell?forum=winserverpowershell

    Overall your question is vague and misleading which has caused many people to post unhelpful responses.  Bill is trying to note that your request is not a specific question.

    Start with the error "Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet," which tells you what the issue is.

    The CmdLet does not exisit on your system.  The answer is to install the module required.   THis is a basic PowerSHell 101 issue.  Perhaps you would do better if you took time to learn basic PowerShell use.


    \_(ツ)_/

    Tuesday, August 27, 2019 5:59 PM