i want to print the client name in powershell - powershell

my original path is D:\FTP\testftp\clientname\Folder\EDW
where under EDW there are files
i am trying to fetch the latest file and oldest file with file count but in output i want client name also but i am getting name of folder
can any one help how to get the client name in output
here is my script
Get-ChildItem -Path 'D:\FTP\testftp\*\EDW' -Recurse -Directory |
Select Name,FullName,
#{Name='Latestfile';Expression = {(Get-ChildItem -Path $_.FullName -Filter *-SALES.csv | Where {$_.lastWriteTime} | select -last 1).Name}},
#{Name='LastestFileTime';Expression = {(Get-ChildItem -Path $_.FullName -Filter *-SALES.csv| Where {$_.lastWriteTime} | select -last 1).lastWriteTime}},
#{Name='OldestFile';Expression = {(Get-ChildItem -Path $_.FullName *-SALES.csv | Where {$_.lastWriteTime} | select -first 1).Name}},
#{Name='OldestFileTime';Expression = {(Get-ChildItem -Path $_.FullName *-SALES.csv | Where {$_.lastWriteTime} | select -first 1).lastWriteTime}},
#{Name='count';Expression = {(Get-ChildItem -Path $_.FullName -File -Recurse| Measure-Object).Count}}
and its output
Name : EDW
FullName : D:\FTP\testftp\clientname\folder\EDW
Latestfile : HIST-SALES.CSV
LastestFileTime : 11/20/2015 11:08:59 AM
OldestFile : -SALES.CSV
OldestFileTime : 11/21/2025 4:08:49 AM
count : 173

You can create another calculated property to include the client name. We can derive that folder from the FullName by splitting the path into parts by the \.
Get-ChildItem -Path 'D:\FTP\testftp\*\EDW' -Recurse -Directory |
Select Name,FullName,
#{Name='Latestfile';Expression = {(Get-ChildItem -Path $_.FullName -Filter *-SALES.csv | Where {$_.lastWriteTime} | select -last 1).Name}},
#{Name='LastestFileTime';Expression = {(Get-ChildItem -Path $_.FullName -Filter *-SALES.csv| Where {$_.lastWriteTime} | select -last 1).lastWriteTime}},
#{Name='OldestFile';Expression = {(Get-ChildItem -Path $_.FullName *-SALES.csv | Where {$_.lastWriteTime} | select -first 1).Name}},
#{Name='OldestFileTime';Expression = {(Get-ChildItem -Path $_.FullName *-SALES.csv | Where {$_.lastWriteTime} | select -first 1).lastWriteTime}},
#{Name='count';Expression = {(Get-ChildItem -Path $_.FullName -File -Recurse| Measure-Object).Count}},
#{Name='Client';Expression={($_.FullName -split '\\')[3]}}

Related

Using PowerShell Where-Object not working with Get-ChildItem

I am trying to get directories that contain "COO", but after adding the Where-Object nothing is returned, the script is looping through each of the folders but nothing makes it to the file when the Where-Object is in place.
This works:
Get-ChildItem -Path $path -Recurse -Directory | select Fullname | Export-csv c:users\shay\desktop\test.csv
This doesn't:
Get-ChildItem -Path $path -Recurse -Directory | Select Fullname | Where-Object {$_.Fullname -like 'COO'} | Export-Csv c:users\shay\desktop\test.csv
In order to use the -Like parameter you'll need to include a wildcard (*), try the following:
Get-ChildItem -Path $path -Recurse -Directory | select Fullname | where-object {$_.Fullname -like '*COO*'} | Export-csv c:users\shay\desktop\test.csv
You could also potentially move the expression to the Get-ChildItem portion of this snippet.
Get-ChildItem -Path $path -Recurse -Directory -Filter '*COO*' | select Fullname | Export-csv c:users\shay\desktop\test.csv

PowerShell cmdlet to get a name of recently zipped folder?

I have tried
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer } | sort CreationTime -desc | select -f 1
but $latest1 is giving blank output.
The -Include switch will only work if the path ends in \* or when used together with the -Recurse switch.
Since in your case, you are only looking for zip files, I would use the -Filter parameter.
$latest1 = Get-ChildItem $path -Filter '*.zip' -File |
Sort-Object CreationTime -Descending | Select-Object -First 1
For PowerShell version below 3.0 use
$latest1 = Get-ChildItem $path -Filter '*.zip' | Where-Object { !$_.PSIsContainer } |
Sort-Object CreationTime -Descending | Select-Object -First 1
If a zip file was found in the path, $latest should now be a FileInfo object with properties like FullName, BaseName etc.
Try changing your where-object filter or removing it
$latest1 = gci $path -Include *.zip| ? { $_.PSIsContainer -eq $false } | sort CreationTime -desc | select -f 1
$latest1 = gci $path -Include *.zip| | sort CreationTime -desc | select -f 1

Split-Path on a selected string

Essentially I want all the information I can produce below to be in the same variable. Currently I have it in 2 pieces. I would like to get the output from the Split-Path as a new column titled "folder" in the $newdata variable.
$newdata = gci -r C:\temp\Screenshots\*.* |
? {$_.LastWriteTime -gt '12/30/16'} |
% {Get-ItemProperty $_} |
select BaseName, Directory
$newdata | select Directory | % {
Split-Path (Split-Path "$_" -Parent) -Leaf
}
That's what calculated properties are for.
$newdata = Get-ChildItem -Recurse C:\temp\Screenshots\*.* |
Where-Object {$_.LastWriteTime -gt '12/30/16'} |
ForEach-Object {Get-ItemProperty $_} |
Select-Object BaseName, Directory,
#{n='Folder';e={Split-Path $_.Directory -Parent | Split-Path -Leaf}}

How to export Txt/Text file to new folder?

This code does taking newest text file from the folder.
$dir = "C:\logsnew\Application"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name
showing like this
C:\Users\kimi> $dir = "C:\logsnew\Application"
C:\Users\kimi> $latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
C:\Users\kimi> $latest.name
4552-4084-63585921993.txt
I would like to put this newest txt file "4552-4084-63585921993.txt" to new folder name "logstop1".
So I try like this:
$dir = "C:\logsnew\Application"
Get-ChildItem -Path $dir |
Sort-Object LastAccessTime -Descending |
Select-Object -First 1 |
Add-Content C:\logsTop1
but this error happens:
Add-Content : Access to the path 'C:\logsTop1' is denied.
How can I fix this problem?
Use Move-Item instead of Add-Content if you want to move the file or Copy-Item if you want to copy it.
Get-ChildItem -Path "C:\logsnew\Application" | Sort-Object LastAccessTime -Descending | Select-Object -First 1 | Move-Item -Destination "C:\logspath1"

powershell get-childitem to csv with details

I am trying to create a CSV file for a file share that has deep folder structure.
I want the CSV to look like:
filename, filepath, file type, folderStructure
So far I have the following:
#Get-ChildItem -Path D:\ -Recurse
$directory="d:\"
gci $directory -recurse |
where {$_.psiscontainer} |
foreach {
get-childitem $_.fullname |
sort creationtime |
select -expand fullname -last 1
}
You don't need to recurse a recursive in Powershell. It will automatically go through all of the subdirectories of subdirectories.
I am also a little unsure of some of the information you wanted, but here is a script that does what you want mostly I believe and is IMO a little better to read.
Get-ChildItem -Path X:\Test -Recurse |`
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName
$Folder = $_.PSIsContainer
$Age = $_.CreationTime
$Path | Select-Object `
#{n="Name";e={$Item}},`
#{n="Created";e={$Age}},`
#{n="filePath";e={$Path}},`
#{n="Extension";e={if($Folder){"Folder"}else{$Type}}}`
}| Export-Csv X:\Test\Results.csv -NoTypeInformation
You will need to change your path, as I created this for a test. My results look like this in Excel:
+-------------------------------+----------------+-------------------------------------+-----------+
| Name | Created | filePath | Extension |
+-------------------------------+----------------+-------------------------------------+-----------+
| test2 | 3/6/2013 19:21 | X:\Test\test2 | Folder |
| Results.csv | 3/6/2013 19:51 | X:\Test\Results.csv | .csv |
| test3 | 3/6/2013 19:21 | X:\Test\test2\test3 | Folder |
| New Text Document.txt | 3/6/2013 19:21 | X:\Test\test2\New Text Document.txt | .txt |
+-------------------------------+----------------+-------------------------------------+-----------+
Where it says "Folder" for the Extension just it returning that it is a directory instead of a blank (No extension).
OK, I changed the way it checks for the parent. It is no longer looking directly at the Parent attribute, and it should correct it now.
Get-ChildItem -Path D:\ -Recurse |`
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName
$ParentS = ($_.Fullname).split("\")
$Parent = $ParentS[#($ParentS.Length - 2)]
$Folder = $_.PSIsContainer
$Age = $_.CreationTime
$Path | Select-Object `
#{n="Name";e={$Item}},`
#{n="Created";e={$Age}},`
#{n="Folder Name";e={if($Parent){$Parent}else{$Parent}}},`
#{n="filePath";e={$Path}},`
#{n="Extension";e={if($Folder){"Folder"}else{$Type}}}`
}| Export-Csv X:\Test\ResultsC.csv -NoTypeInformation
It is now taking the path to the current item, turning it into an array by splitting on the \, and then giving you the value at ArryLength - 2
I am not sure if this is the best approach but it works. I have a feeling code could be shorten to get the full path of a file.
$myDirectory = "D:\"
Get-ChildItem -Path $myDirectory -Recurse |`
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName
$ParentS = ($_.Fullname).split("\")
$Parent = $ParentS[#($ParentS.Length - 2)]
$ParentPath = $_.PSParentPath
$ParentPathSplit = ($_.PSParentPath).split("::")
$ParentPathFinal = $ParentPathSplit[#($ParentPathSplit.Length -1)]
#$ParentPath = [io.path]::GetDirectoryName($myDirectory)
$Folder = $_.PSIsContainer
$Age = $_.CreationTime
$Path | Select-Object `
#{n="Name";e={$Item}},`
#{n="Created";e={$Age}},`
#{n="Folder Name";e={if($Parent){$Parent}else{$Parent}}},`
#{n="filePath";e={$Path}},`
#{n="Extension";e={if($Folder){"Folder"}else{$Type}}},`
#{n="Folder Name 2";e={if($Parent){$Parent}else{$Parent}}},`
##{n="Folder Path";e={$ParentPath}},`
#{n="Folder Path 2";e={$ParentPathFinal}}`
}| Export-Csv d:\ResultsC_2_F.csv -NoTypeInformation