1

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}
PSNewb
  • 35
  • 7
  • The code finds directories that are empty, i.e, those who contain neither files nor subdirectories (whether those subdirectories are empty or not). While the code can be optimized, it looks functionally correct; the only caveat is that the `Get-ChildItem` command will not include _hidden_ subdirectories; to that end, use `-Force`. If you still encounter unexplained behavior, please try to provide an [MCVE (Minimal, Complete, and Verifiable Example)](http://stackoverflow.com/help/mcve) or as close as you can get to one. – mklement0 Sep 23 '19 at 01:47
  • If, by contrast, you were looking for _recursively_ empty directories - i.e. those that contain either nothing or only empty subdirectories, recursively - see https://stackoverflow.com/a/42861120/45375 – mklement0 Sep 23 '19 at 01:48
  • @mklement0 Thank you for the content. Great info as I go forward. Your "Counting Folder Depth with PowerShell" is what I wanted to do. Trying to run it I've seen I don't have admin privileges (Don't have permissions for `Set-ExecutionPolicy` as well). I get the gist of it. I can give it a path `$LiteralPath`, it checks if there are files or not `$haveFiles = [bool]...` and does a recursive search through the folders... which I won't even try to act like I understand. I'm still fairly new, how would I write this not as a "Parametrized" script? Thank you for any help. – PSNewb Sep 23 '19 at 18:38
  • You can use `Set-ExecutionPolicy -Scope CurrentUser ...` to change the execution policy just for you, which doesn't require admin privileges. For the recursion to work you do need a defined unit of functionality (script or function). – mklement0 Sep 23 '19 at 18:58
  • @mklement0 Still doesn't work for me unfortunately. They keep things pretty tight here. Keep getting `Set-ExecutionPolicy : Attempted to perform an unauthorized operation. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option...` which I'm blocked from doing. – PSNewb Sep 24 '19 at 11:24
  • That's why I suggested `-Scope CurrentUser`, for which you don't need to run as admin. – mklement0 Sep 24 '19 at 12:33
  • Ok got it. When I do it I get `cmdlet Set-ExecutionPolicy at command pipeline position 1 Supply values for the following parameters: ExecutionPolicy: ` none of the options `(Unrestricted | RemoteSigned | AllSigned | Restricted | Default | Bypass | Undefined)` work and I get that error. The files are on a shared drive as well, apologize for leaving that detail out if it helps give more clarrity. – PSNewb Sep 24 '19 at 13:23
  • `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force` should work, for instance. If it doesn't, there's something very unusual / wrong with your setup. – mklement0 Sep 24 '19 at 13:34
  • @mklement0 Yea it didn't work. They keep those things tight here. And Unfortunately I'm not in out IT, just found PowerShell made my tasks easier. Thank you again. – PSNewb Sep 24 '19 at 14:19
  • 1
    Not that I'm suggesting you circumvent regulations, but in a pinch you can use the PowerShell CLI to bypass the execution policy in effect; e.g., `powershell -ExecutionPolicy Bypass -Command "'hi'; get-date"` or `powershell -ExecutionPolicy Bypass -File someScript.ps1` – mklement0 Sep 24 '19 at 14:31

1 Answers1

1

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}
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • Thank you but still having issues. I just realized I don't have Admin privileges or `Set-ExecutionPolicy`. That could be part of the problem. – PSNewb Sep 23 '19 at 18:42