Question

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.

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

Get-ChildItem –Path "C:/Users/username/Documents/Test/" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-180))} | Remove-Item -Recurse -Force

Get-ChildItem –Path "C:/Users/username/Documents/Test/" -Recurse

Get-ChildItem - this commands gets all the sub directories and files

-Path option specifies the directory from where to start

-Recurse option allows to go to subdirectories and files

| "pipe" - the output from Get-ChildItem is passed to Where-Object

Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-180))}

Where-Object - for each object

$_.LastWriteTime - get the last write time

-lt - less than

Get-Date - get the current days

AddDays - add the specified number of days

for each directories/file from the Get-ChildItem check the lastwriteTime it's older than 180 days/6 months

| "pipe" - pass the output from Where-Object to Remove-Item

Remove-Item -Recurse -Force

Remove-Item - removes an item(file/folder)

-Recurse - Tranverse to sub folders

-Force - forcefully

Add a comment
Know the answer?
Add Answer to:
I am trying to write a Powershell script that will automate the deletion process of certain...
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
  • 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...

  • 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...

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

  • i am trying to write a subroutine in assembly that removes all occurrences of a given...

    i am trying to write a subroutine in assembly that removes all occurrences of a given character in a string. The subroutine takes two parameters: the string pointer, and the character to be removed. Write a C code that calls this subroutine. The string is defined in the C source code file as global. Assume the assembly and C code are in separate files. Use Keil/uVision to test your program where the C code should ask the assembly subroutine to...

  • 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 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...

  • ^^^ Q3. I am trying to implement double linked list but I was failed to write...

    ^^^ Q3. I am trying to implement double linked list but I was failed to write the code so anyone gives the main Code in the main function   THANK YOU FOR ADVANCE #include<stdio.h> #include<stdlib.h> #include<alloc.h> struct node {      int info;      struct node *lptr,*rptr; }; typedef struct node DL; DL *delete( ) , *insert ( ); void display(); DL *delete(DL *start,int x) {      DL *left,*right,*curr;      curr = start;      if( start == NULL)       {                 printf("\nDoubly...

  • I am trying to do this assignment: Write a program that reads a list of concepts/strings...

    I am trying to do this assignment: Write a program that reads a list of concepts/strings from the LIST.txt text file (one concept per line), stores them into an array of strings, and then prints the following: 1.Prints a heading “LIST1:” and then prints all the concepts from the array, one concept per line 2.Prints a heading “LIST2:” and then prints all the concepts from the array that do not contain spaces, one concept per line 3.Prints a heading “LIST3:”...

  • Completion is a screenshot of the batch file showing all of the steps to create the file system s...

    Completion is a screenshot of the batch file showing all of the steps to create the file system shown below. Instructions Create a batch file clifilesystem.cmd using Notepad which performs the following steps to create the file system shown below. Also use a proper comment for each step before the command. Proof your work. Finally submit the clifilesystem.cmd file in Blackboard Figure 1. The clijfilesystem.cmd file system Work from Desktop unless using an absolute path Desktop NOS110 OS Labs Homework...

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
Active Questions
ADVERTISEMENT