Question

Hello I have a task with Powershell. I need a script to delete files by name...

Hello

I have a task with Powershell. I need a script to delete files by name in a folder located on SharePoint on-premise. The files start with specific later and ended with

_final.

what we need:

Only focus on files that start with BLG_STOCK.

We need 1 file per month, based on the filename (not the publishing date):

  • for the current month: only the last file (today).
  • For every previous month: only the last file of that month needs to remain. We are allowed to add something like _FINAL (if that would help us).

Yes, I only need to have the "BLG_STOCK_final" per month. All others files starting with BLG_STOCK (without _final) need to be deleted.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the best answer for this question:

I have created a folder named HomeworkLib with below files in my Document library:

I executed the script from powershell:

Please enter your sharepoint online username and password when asked.

Script execution status:

So script completed without any issues.

We will check the sharepoint library now

It deleted all the files except BIG_STOCK_final.

Script code:

Code in text format:

# Input Parameters. Please update both as per your needs.  
$siteURL="https://xxxxxxx.sharepoint.com/sites/mytestsite"   
$folder="Shared Documents/HomeworkLib"
  
# Function to get all the files details in the given folder
Function GetFiles($folderUrl)
{
   # Getting files details
$FilesList=Get-PnPFolderItem -FolderSiteRelativeUrl $folderUrl -ItemType File

   # If files are not there, then showing a message and exiting
if($FilesList.Count -eq 0)
{
   write-host -ForegroundColor Green 'No files are available'
break;
}
else
{
# If files are there, then reading each files name
foreach($file in $FilesList)
{   
           # deleting the file if file name is not 'BLG_STOCK_final'
           IF (Compare-Object $file.Name "BLG_STOCK_final")
           {
                  
               $deleteFile= $folderUrl+"/"+$file.Name
               Remove-PnPFile -SiteRelativeUrl $deleteFile -Force

           }
       }
   }
}
  
# Connect to SharePoint Online site   
Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential)
  
# Call the function to get the files details
GetFiles($folder)

Notes:

  • Update the script with correct site URL
  • Update the script with correct folder path where you have placed the files
  • Test the script with some sample data before executing it on actual files
  • If Pnp module is not installed on your system, then open powershell and enter the below command:

Install-Module SharePointPnPPowerShellOnline

Please hit the like button if my answer was helpful. Thanks in advance.

Add a comment
Know the answer?
Add Answer to:
Hello I have a task with Powershell. I need a script to delete files by name...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to write a Powershell script that will automate the deletion process of certain...

    I am trying to write a Powershell script that will automate the deletion process of certain files. These files are older than 6 months old, and need to be deleted. If my $Path = "C:\Users\username\Documents\Test\", what would be the Powershell script to DELETE every file and directory older than 6 months old in the 'Test' directory. I am new to Powershell, please explain the parts of the script as well.

  • i need help with this scripting assignment in power shell thanks Lab 7: PowerShell Search and...

    i need help with this scripting assignment in power shell thanks Lab 7: PowerShell Search and Report Search Report host path date Execution time nnn Directories n, nnn, nnn Files nnnnnnn Sym links nnnnnnn Old files nnnnnnn Large files nnnn, nnn Graphics files nnnnnnn Executable files nnnnnnn Temporary files n, nnn, nnn TotalFileSize nnnn, nnn Introduction Lab 7 is nearly identical to Lab 2, except it is written in PowerShell, uses no UNIX/Linux tools to do its work, creates no...

  • Hello I need help creating a bash script in linux for the following question: Create a...

    Hello I need help creating a bash script in linux for the following question: Create a script to expand the jpeg creator to create other types of files such as pdf, png, gif, etc. The jpeg creator is the following: #!/bin/bash FILE=$1 echo -e -n "\xFF\xD8\xFF\xE0"> $FILE dd if=/dev/random bs=512 count=4 >> $FILE

  • Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If...

    Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If you could start the assignment and tell me how to do the rest it would be greatly appreciated! (Quick thumbs up answer response if thorough and correct) Maintain automobile records in a database Write a shell script to create, view and modify a simple database that contains automobile records. The shell script has to be done in Bourne shell syntax (bash as a matter...

  • Hello, please help with the following, you're tasked with writing a shell script named “rpsm.sh”....

    Hello, please help with the following, you're tasked with writing a shell script named “rpsm.sh”. The script reports and prints (to stdout) selected storage management information. Think of it as “RePort Storage Management”. The easy way to describe what the script needs to do is to look at what it should display in the case where you provide incorrect parameters, or in the case where you provide “-h” for help: Usage: ./rpsm.sh <options>< directory> Reports selected information about specified directory...

  • The first script is validate.sh. This is a simple form validation script that will be used...

    The first script is validate.sh. This is a simple form validation script that will be used to verify the inputs given. Normally, this would be done to validate input from a website or another program before entry into a database or other record storage. In this case, we will keep it simple and only focus on the input validation step. In particular, the script should prompt the user for four values: first name, last name, zip code, and email address....

  • I am a beginner in shell. Please help me with this question. question: In this assignment,...

    I am a beginner in shell. Please help me with this question. question: In this assignment, you will research into a few standard Unix utilities and leverage them in a shell script to perform a simple but useful task that's right up the alley of Unix shell scripting. As usual, you should aim for reasonably efficient algorithms and reasonably organized, comprehensible code. Consolidating log files My Internet chat software recording my conversations into daily log files, each named by date,...

  • Hello, so I need someone to look over this code and see if im on the...

    Hello, so I need someone to look over this code and see if im on the right path. I feel like im missing something. Here is the assignment: Create a class called Slogans. This class will extend the Thread class. This thread will read from a file several quotes that are on each line. It will pick a quote randomly and display it to the console. It will change quotes every 7 seconds. This will be updated in the final...

  • I need help writing this C code. Here is the description: Let's say we have a...

    I need help writing this C code. Here is the description: Let's say we have a program called smart typing which does two things: The first letter of a word is always input manually by a keystroke. When a sequence of n letters has been typed: c1c2...cn and the set of words in the dictionary that start with that sequence also have c1c2...cnc then the smart typing displays c automatically. we would like to know,  for each word in the dictionary,...

  • Hello! I need help setting my program up to initiate certain tasks! We are using a...

    Hello! I need help setting my program up to initiate certain tasks! We are using a Centos Server as a root user! Use the CentOS Server and as root user perform the following: Setup two ext4 partitions on /dev/sdc at 1 GB each Mount sdc1 to /var/www (create any directories as needed) Mount sdc2 to /var/data (create any directories as needed) Install the httpd package Create a file called final.txt in /var/www/html (in that put your name/date on the first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT