Compiling CSV's dependent on Date - powershell

this is my first post so sorry about any mistakes.
I'm currently trying to use Powershell to combine folders of csv files based on date. I'm trying to go a week back, compile them, and export to another folder. I've only been using Powershell a few days and have an ok knowledge on coding in general.
I'm trying to use this fuction:
(Get-ChildItem C:\Folder | Group-Object -AsHashTable {$_.CreationTime -ge (Get-Date).AddDays(-2)})
That outputs a name(true or false) and a value(folder title). What I want to do is use another function to then export those CSV files in several folders all to one folder.
Is this possible? Am I going in the right direction? I have very little experience with this.
Thanks.
Luke

You're looking to filter by instead of group by, hence you would be using Where-Object instead of Group-Object. To copy files you can use the built-in cmdlet Copy-Item.
Do note, the path\to\destinationfolder in the example below must be an existing folder, you should create it before running the code.
# NOTE: If you want to filter only for files with .CSV Extension,
# `-Filter *.csv` should be included
Get-ChildItem C:\Folder -Recurse |
Where-Object { $_.CreationTime -ge (Get-Date).AddDays(-2) } |
Copy-Item -Destination path\to\destinationfolder

Related

Powershell: How to copy files from one Directory to another

I am looking to copy a series of files from one directory to another. Essentially the files are a series of zip folders that are simply changed versions of programs. The files will be named something like: test_1_092.zip in the source directory and test_1_091.zip in the target directory. I don't want the script to look at the numeric portion of the folder, simply the name.
Please forgive my lack of knowledge as this is my first foray into powershell scripting. Any thoughts or need more info?
Something to start with
(Get-ChildItem -Filter "*.zip").Name | where {$_ -like 'test_1_*'} | Move-Item -Destination .\1 -Force -WhatIf
Please confirm the output from –whatif , then remove it to perform the action.
For the relation between the new and old name, please provide some more information.

Could you help me with a script (made on Powershell) to delete files (eventualy subfolders) of a specific folder that are older than 5 days?

As I said in the question, I have tried to create a script that would delete files and subfolders of a folder that are older than 5 days.
I am new to Powershell, I have already seen some tutorials and read about the syntax of it, but that has not helped me much, so I just found a code that is alike the one I need and I have tried to adapt it to my needs. I will paste it here:
$limit = (Get-Date).AddDays(-5)
$path = "C:\Users\Me\Desktop\example"
Get-ChildItem -Path $path -Recurse | Where-Object {$_.PSIsContainer -and
$_.CreationTime -lt $limit} | Remove-Item
The script has runned, without any error message, but the files have not been deleted. I would like to know what may be the problem. By the way, would this script delete subfolders as well? Other thing, how do I do to schedule this script?
Note: As I said, I am a beginner, so if someone could clarify these objects for me, I would be very glad: "PSIsContainer", "CreationTime", "|". I have already search what this "$_." means, but I have not understood it well yet, so if someone could tell me it directly, I would be very thankful as well. Thank you in advance.
I have tried a different code structure, but that basically does the same thing, and it has worked:
$Now=Get-Date
Get-Childitem C:\Users\Me\Downloads | Where-Object { $_.LastWriteTime –lt
$Now.AddDays(-5) } | Remove-Item
Now I am just not sure if it will delete subfolders as well, and I still need to schedule it, but I will keep on trying it and if I get it, I will bring some update about it here.

How to print into a csv from Powershell?

I am using this command to get all of the child folders and child-child folders and so on.
Get-ChildItem -dir S:\WGroups -Recurse.
But what I would like to do is print it either into a csv or a txt file and was wonder what the best way to do that?
Get-ChildItem -dir S:\WGroups -Recurse | export-csv -Path $env:userprofile\documents\S_wgroups.csv
There are a number of fields you may not want, so filtering it to just what you need may be important if you're attempting to process in an automated fashion.

get the latest created folder from a path using powershell

How to get the latest created folder from a path using Windows PowerShell?
I have the path C:\temp and I want to find the most recently created folder in this path.
PowerShell works mainly with the pipeline, so most of what you'd write will consist of creating objects representing some information, and filtering and manipulating them. In this case, the objects are a bunch of folders.
Get all items in the folder. This will get files and folders, that's why step 2 is necessary. The | at the end of the line signals that the pipeline will continue in the next line – objects created by Get-ChildItem will then be passed one by one to another command.
Get-ChildItem c:\temp |
Filter for folders. There is no really elegant way, sadly. Don't worry about that it says “container”, not “folder” – Those commands work with many different things, not only files and folders, so a more general concept was used in naming.
Where { $_.PSIsContainer } |
Sort by date, descending, so the newest folder is the first one.
Sort CreationTime -Descending |
Select the first (newest) folder.
Select -First 1
So in short:
gci c:\temp | ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1
or
(gci c:\temp | ? { $_.PSIsContainer } | sort CreationTime)[-1]
Both of those lines make heavy use of default aliases for commands in PowerShell, such as ? for Where-Object. You should use the full names in scripts, though, as you'll never know what the aliases will look like on other machines the code might run on.
EDIT: PowerShell 3 has additional parameters for Get-ChildItem that allow you to do filtering for files or folders directly, so you don't need the Where:
Get-ChildItem -Directory C:\temp | ...
Generally you will work with objects and their properties in PowerShell. Two very helpful commands are Get-Member and its alias gm and Get-Command or just gcm. Get-Member will tell you what properties and methods an object has; you just pipe something else into it for that:
Get-ChildItem | gm
will tell you what properties files and directories have.
Get-Command will list all commands there are or those that match a particular pattern. PowerShell commands try to be very consistent in their use of verbs and nouns. To find all commands that end in Object you can try gcm *-Object – those are general commands working with pretty much everything. Get-Help ForEach-Object then would tell you about a particular command, ForEach-Object in this case.

Find files only in subdirectories named OLD with a certain age using PowerShell

How can I get a nice list of files in a directory tree that contains multiple OLD Files?
I'd like to see only files from directories named OLD that have a certain age.
As I understand the question, Raoul Supercopter's solution doesn't quite answer it. Instead of finding all files from directories that are named "OLD" the solution above finds all files that contain "OLD" in their name.
Instead, I think you're asking for something that finds all files older than a certain date that are in directories named OLD.
So, to find the directories, we need something like the following:
dir -r | ? {$_.name -match "\bold\b" -and $_.PSIsContainer}
But you then need something that can recursively go through each directory and find the files (and, potentially, any directories named "OLD" that are contained in other directories named "OLD").
The easiest way to do this is to write a function and then call it recursively, but here's a way to do it on one line that takes a different tactic (note the line continuation character so this would fit:
dir -r | ? {!$_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date 5/1/2006)} `
| ? {(% {$_.directoryname} | split-path -leaf) -eq "OLD"}
So, what's happening here?
The first section is just a basic recursive directory listing. The next section checks to be sure that you're looking at a file (!$_.PSIsContainer) and that it meets your age requirements. The parentheses around the Get-Date section let you get the results of running the command. Then we get the directory name of each file and use the split-path cmdlet to get just the name of the closest directory. If that is "OLD" then we have a file that matches your requirements.
Well, the first part is to list all the files, the second part filter your files, and finally format your output. You can use Format-List or table (but I don't have a PowerShell installation nearby to test it :])
$yourDate = Get-Date 5/1/2006
ls -recurse . | ? { $_.fullname -match "OLD" -and $_.lastwritetime -lt $yourDate } | % { $_.fullname }
Get-Date creates a Date-Time object when you give it a specific date as a parameter. Just use it and filter.