I would like list all path to go to folder name ending with "_S" recursively.
I did that:
Get-ChildItem -Recurse | Where-Object {($_.Attributes -match "Directory") -and ($_.Name.EndsWith("_S") -and ($_.PSIsContainer -eq 1))}
But the result isn't an array. How i can to exploit the results ?
My goal is to have something like that:
Myfolder\folder1\folder1_S
Myfolder\folder2_S
Use Select-Object and grab the FullName of the file.
Also, as stated in the comments on the question by #Paul ($_.Attributes -match "Directory") and ($_.PSIsContainer -eq 1) is redundant, might want to remove one of them.
Get-ChildItem -Recurse | Where-Object {($_.Attributes -match "Directory") -and ($_.Name.EndsWith("_S"))} | Select-Object -ExpandProperty FullName
The above can also be refactored in PowerShell 3.0+, to
Get-ChildItem -Recurse -Directory -Filter *_S | Select-Object -ExpandProperty FullName
which would recursively get the path of all directories ending with "_S"
Related
I am writing a PowerShell script to sort the directory and return folders with the names that start with the number sequence that is greater than specified sequence, but -gt operator acts as a -ge operator.
Here is the code I'm running:
Get-ChildItem C:\Users\USER\Testing -recurse |
Where-Object { $_.PSIsContainer -ge $true -and $_.Name -gt "003" -and $_.Name -match '^\d+.*$' } |
Select-Object Name
The response I get is:
005-folder
003-folder
004-folder
There seems to be the similar but opposite pattern for -le and -lt operators. Both of them do NOT include the equal item. So when I run
Get-ChildItem C:\Users\USER\Testing -recurse |
Where-Object {$_.PSIsContainer -ge $true -and $_.Name -le "003" -and $_.Name -match '^\d+.*$' } |
Select-Object Name
The response I get is:
001-folder
002-folder
I can't seem to find anything on the internet that solves the issue I am having, so I assume something in my scripts breaks the -gt and -le operators?
I think I figured out the issue few minutes after I posted it. '003-folder' will be greater than '003' cause I'm comparing strings. Need to truncate first.
Get-ChildItem C:\Users\USER\Testing -recurse -Directory |
Where-Object {$_.Name.subString(0, 3) -gt '003' -and $_.Name -match '^\d+.*$' } |
Select-Object Name
I have code I put together from different sources found online. That code, I believe, is supposed to search and find all folders who have no files/and whose subfolders have no files, then add the text "NA -" to the beginning of these folders. When I checked it looked like some folders that were obviously empty were not marked. Is it something I'm missing?? Any help would be greatly appreciated, thank you.
$CMfolder=get-childitem "Z:\folder\subfolder\subfolder2" -Recurse |
Where-Object {$_.PsIsContainer -eq $true}
$CMfolder | Where-Object {$_.GetFiles().Count -eq 0 -and
$_.GetDirectories().count -eq 0} |
where-object {$_.Name -Notlike "NA -*"} |
Rename-Item -NewName {"NA -" + $_.Name}
try this :
Get-ChildItem "c:\temp" -recurse -directory -Force | where Name -Notlike "NA -*" |
select *, #{N="HasChild";E={((Get-ChildItem $_.FullName -recurse -force -file).Count -ne 0)}} | where {! $_.HasChild}
I'm trying to open the most recent picture from all my folders in my onedrive using powershell. I have already gotten to the point where i have a "list" which automatically sorts to newest and only gives me the "first 1" result.
I've tried implementing "Select-Object ().Open" but i don't see where i need to implement it. I can't get it to work.
I've also tried taking the directory and filename in variables to try and paste these together to then use these to open the picture.
This is my command:
Get-ChildItem -Recurse | Where-Object {$_.Name -match "(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge "03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1
I'm expecting that there is a way to do this action and open this file in 1 command.
EDIT: I just discovered "Fullname" in the "Select-Object" function which is my directory and filename togheter. I still don't know how to implement ".Open" with this though.
Try this -
$FirstFile = Get-ChildItem -Recurse | Where-Object {$_.Name -match "(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge "03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1
Start-Process $FirstFile.FullName
You can also put the whole thing into a single line, though breaking the code will provide more readability -
Start-Process (Get-ChildItem -Recurse | Where-Object {$_.Name -match "`
(.jpg|.png)" -and $_.CreationTime.Year -ge "2019" -and $_.CreationTime.Month -ge`
"03"} | Sort-Object LastWriteTime -Descending | Select-Object -first 1).FullName
I was wondering how I can display a list of empty files in a directory
$test = gci "C:\Users\Freedom\Documents" -Recurse
$test | Where-Object {$_.PsISContainer} | Select-Object FullName | Where-Object {$_.GetFiles() -eq 0}
I Don't understand because when I do get-childitem | get-member I get a list of properties and methods I can use and in the list is getfiles() why can't I use this method why's it giving me an error message?
Method invocation failed because [System.IO.FileInfo] does not contain a method named 'GetFiles'.
I think you want this:
Get-ChildItem | Where-Object { (-not $_.PSIsContainer) -and ($_.Length -eq 0) }
If you have PowerShell 3.0 or later you can use this:
Get-ChildItem -File | Where-Object { $_.Length -eq 0 }
Of course you can add whatever other parameters for Get-ChildItem that you want (-Recurse, etc.).
Wow I had what I wanted mixed up! And I had to add the .count to the getfiles() method
$test | Where-Object {$_.PsISContainer} | Where-Object {$_.GetFiles().Count -eq 0} | Select-Object FullName
try this
Get-ChildItem "c:\temp" -File -Recurse | where Length -eq 0
Use Get-ChildItem and the File flag, -Recurse is needed to get every file in the folder and in the folder below. Then get all the files were Get-Content returns null.
Get-ChildItem $YourPath -Recurse -File | Where-Object {!(Get-Content $_.Fullname)}
I was wondering if is possible to specify multiple search filters as once. For example, I have this line of code that finds all files that have the "&" symbol.
get-childitem ./ -Recurse -Filter "*&*" |
? { $_.PSIsContainer } |
Select-Object -Property FullName
I'd like to extend this so that I can search one time and find files with other symbols like %,$,#, etc. I want to find files that have any of these symbols, not neccesarly files that have all of them so I assume there needs to be an OR somewhere. I tried the below code but it didn't seem to work for me:
get-childitem ./ -Recurse -Filter "*&*" -Filter "%" |
? { $_.PSIsContainer } |
Select-Object -Property FullName
You can use the -match operator and a regex for this:
Get-ChildItem -Recurse |
Where { !$_.PSIsContainer -and ($_.name -match '&|%|\$|#')} |
Select-Object -Property FullName
If you are on PowerShell v3 or higher you can simplify this a bit:
Get-ChildItem -Recurse -File |
Where Name -match '&|%|\$|#' |
Select-Object -Property FullName
If you have V3 or better, you can leverage the "globbing" wildcard feature:
get-childitem './*[&%$#]*' -Recurse | where {$_.PSIsContainer}
If you've got V4, you can dispense with the $_.PSIsContainer filter and use the -Directory switch:
get-childitem './*[&%$#]*' -Recurse -Directory