Get-ChildItem to textfile without fullname - powershell

I need to sort all Subdirectorys with files from a certain folder to a file.
If I use
Get-ChildItem -recurse | Select-Object FullName > C:\text\7.txt
I end up with a problem. The file created starts with
FullName
----
which I don't want. The file created is used in a much larger script I'm creating.
I want it all listed in the .txt like this
D:\Logs\Application.evtx
Just folder\filename and no FullName etc.

Related

Powershell - Extract first line from CSV files and export the results

Thanks in advance for the help.
I have a folder with multiple CSV files. I’d like to be able to extract the first line of each of the files and store the results in a separate CSV file. The newly created CSV file will have the first column as the file name and the second column to be the first line of the file.
The output should look something like this (as an exported CSV File):
FileName,FirstLine
FileName1,Col1,Col2,Col3
FileName2,Col1,Col2,Col3
Notes:
There are other files that should be ignored. I’d like the code to loop through all CSV files which match the name pattern. I’m able to locate the files using the below code:
$targetDir ="C:\CSV_Testing\"
Get-ChildItem -Path $targetDir -Recurse -Filter "em*"
I’m also able to read the first line of one file with the below code:
Get-Content C: \CSV_Testing\testing.csv | Select -First 1
I guess I just need someone to help with looping through the files and exporting the results. Is anyone able to assist?
Thanks
You basically need a loop, to enumerate each file, for this you can use ForEach-Object, then to construct the output you need to instantiate new objects, for that [pscustomobject] is the easiest choice, then Export-Csv will convert those objects into CSV.
$targetDir = "C:\CSV_Testing"
Get-ChildItem -Path $targetDir -Recurse -Filter "em*.csv" | ForEach-Object {
[pscustomobject]#{
FileName = $_.Name
FirstLine = $_ | Get-Content -TotalCount 1
}
} | Export-Csv path\to\theResult.csv -NoTypeInformation
I have assumed the files actually have the .Csv extension hence changed your filter to -Filter "em*.csv", if that's not the case you could use the filter as you currently have it.

Save directory tree to CSV, along with whether element is a file or folder in powershell

So far I've got this:
Get-ChildItem -Recurse 'C:\MyFolder' |
Select-Object FullName, name |
Export-Csv -path 'C:\output.csv' -noTypeInfo
It gives me a CSV with the full path name, and name of each folder & file in a directory.
However, I'd like a 3rd column that has either 'Folder' or 'File' (or something similar). Basically just a column that explicitly tells me whether something is a file or folder.
I feel like it should be a simple case of adding a new column like FullName, name, Type - but not sure what options are available.
Is this possible?
You could use the Attributes property however, this might give you more information than you really need, see FileAttributes Enum.
If you simply need a property with 2 values (File or Directory) you can use the boolean property PSIsContainer as reference to construct your new calculated property:
Get-ChildItem -Recurse 'C:\MyFolder' |
Select-Object FullName, Name, #{N='Type';E={('File', 'Directory')[$_.PSIsContainer]}} |
Export-Csv -path 'C:\output.csv' -NoTypeInformation

How to use Get-Content to get all information from the most recent file

I am trying to use Get-Content to get the most recent .xml file and all its content to be displayed in the powershell window, but I am having a hard time.
I have use the the following:
Get-ChildItem "\\Server1\c$\Program Files\AAA\Logs\" | Sort-Object CreationTime | Select-Object -Last 1
Get-Content -Path "\\Server1\c$\Program Files\AAA\Logs\" | Where-Object {$_.LastWriteTime -lt (get-date).addDays(-1)} | Select -Last 1
But I cannot figure out how to go about grabbing the latest file and displaying all its content in the console
You are close. You have to pipe the result of your first line to Get-Content:
Get-ChildItem "\\Server1\c$\Program Files\AAA\Logs\" | Sort-Object CreationTime | Select-Object -Last 1 | Get-Content
Your second line does not make much sense. If you provide a valid path to Get-Content, it will return to you the content of the file as a string. You cannot apply any creation time logic to this content afterwards with Where-Object.
Your first line though, works like this:
It gets all files and folders that are contained in your given path. If this path really just contains valid log files, you can leave it like this. Otherwise you should filter this result, so you really just get your desired files. To be precise, Get-ChildItem returns an array of System.IO.FileInfo objects. They contain a lot of information about your files.
You then sort this array of System.IO.FileInfo objects by the CreationTime property with Sort-Object.
Finally, you select the last element of the sorted array. This is still a System.IO.FileInfo object. That's why you see some of its properties in your output.
If you then pipe this System.IO.FileInfo object to Get-Content, the FullPath property of this object will be mapped to the -Path parameter of Get-Content, thus returning the content of the file specified by the System.IO.FileInfo object.

Powershell - using dir / GetChildItem to list directories with Date Modified

So currently I am able to get a list of my directories with the folder name using
dir -directory -name
And I also know I can use recurse as well to list sub-directories
Thank you to Sany
What I would like to create in this list is to show this folder's date modified value. I've looked over the documentation and I'm having difficulty to find the answer I assume if it is there it is likely part of Attributes but I'm uncertain as to how to format it correctly.
Most of the searches I've done are have turned up about excluding files based on date modified, rather than showing the date instead.
You can use Select-Object and what I like to use Export-Csv
Get-ChildItem C:/temp -directory -recurse | Select-Object FullName, LastWriteTime | Export-Csv -Path list_my_folders.csv -NoTypeInformation
In case you want extract other information as well you can also remove the Select-Object part and you will see all columns which you can select.
Output:
"FullName","LastWriteTime"
"C:\temp\save","21.11.2019 15:34:27"
"C:\temp\test","12.01.2020 05:13:24"
"C:\temp\test\002custom","14.12.2019 01:17:54"
"C:\temp\test\002normal","14.12.2019 01:31:46"
"C:\temp\test\x","13.01.2020 12:51:05"
"C:\temp\test\002normal\normal","14.12.2019 01:31:53"
"C:\temp\test\x\Neuer Ordner","13.01.2020 12:51:05"
Of course you can also use it without Export-Csv:
Get-ChildItem C:/temp -directory -recurse | Select-Object FullName, LastWriteTime > list_my_folders.txt
But the output is in a format that is harder to work in most cases:
FullName LastWriteTime
-------- -------------
C:\temp\save 21.11.2019 15:34:27
C:\temp\test 12.01.2020 05:13:24
C:\temp\test\002custom 14.12.2019 01:17:54
C:\temp\test\002normal 14.12.2019 01:31:46
C:\temp\test\x 13.01.2020 12:51:05
C:\temp\test\002normal\normal 14.12.2019 01:31:53
C:\temp\test\x\Neuer Ordner 13.01.2020 12:51:05

Power shell display files in folder containing specific names

I have a list of pdf file names which i have stored in a text file line by line as shown below.
322223491.pdf
322223492.pdf
322223493.pdf
the name of the text file is inclusions.txt.
I am passing this to a variable:
$inclusion = get-content .\inclusions.txt
Now i want to check and display if a folder (C:\users\xyz) contains files passed in $inclusions variable how do i do that?
PS C:\users\xyz> Get-childitem
command displays the following
Mode LastWritetime Name
The Name column is the property of interest to me where in i want to compare my inclusions list and if its found in the list then i want to display them in the Powershell terminal
PS C:\users\xyz> Get-childitem | Select-Object -Property Name
is as far as i have got my head around it i dont know how to proceed further to display the filtered data by comparing it with the $inclusions variable.
Furthermore i want to delete the files if they match the name in the inclusions text file. So that is the final Objective to traverse through all the files in the folder compare if the name is same as in the inclusions list and if yes then delete those particular files.
Any help would be appreciated.
Many thanks
Use Where-Object to filter on a condition:
Get-ChildItem |Where-Object { $inclusion -contains $_.Name }
If you are only interested in the file names themselves, use Select-Object -ExpandProperty to grab just the name of each file:
Get-ChildItem |Where-Object { $inclusion -contains $_.Name } |Select-Object -ExpandProperty Name
If you want to do something based on whether a certain folder contains one or more of the files in $inclusion, use an if statement:
$folderPath = "C:\users\xyz\folder\of\interest"
if(#(Get-ChildItem $folderPath |Where-Object {$inclusion -contains $_.Name}).Count -ge 1)
{
Write-Host "Folder $folderPath contains at least one of the inclusion files"
}