The simplest and fastest solution (PSv3+) is to use the -Filter
parameter:
Get-ChildItem -File -Filter *.
as explained in this answer.
As for a pipeline-based solution:
PowerShell (Core) 7 now offers the -Not
switch with the simplified comparison statement syntax that itself was introduced to Where-Object
(whose built-in alias is ?
) in Windows PowerShell v3, which enables the following, more concise variant of TheMadTechnician's helpful answer:
Get-ChildItem -File | Where-Object -Not Extension
# Alternatively, using built-in alias ? for Where-Object
Get-ChildItem -File | ? -Not Extension
As in TheMadTechnician's answer, this uses implicit Boolean logic: any nonempty string evaluates to $True
in a Boolean context, so applying -Not
(whose alternative form in expression(-parsing) mode is !
) means that $true
is only returned if the string is empty, i.e., if the file has no extension.
As for what you tried:
gci | where {$_.extension -eq ""}
works in principle, except that it also includes directories.
In PSv3+ you can use the -File
switch to limit the results to files, as above.
In PSv2 you must add a condition to the script block, based on the .PSISContainer
property only returning $True
for directories:
gci | where { -not $_.PSIsContainer -and $_.Extension -eq "" }
gci -File -Recurse | ?{!($_.Extension)} | Rename-Item -NewName {$_.BaseName + '.<ext>'}
, easy peasy!. – Fresno