
close
close
Chance to win $250 in Petri 2023 Audience Survey
Here’s another problem that recently came across my desk. The need was to identify files with long names. Even if you don’t have this need, these problem solving articles will hopefully demonstrate PowerShell techniques and concepts that you can use in other projects or tasks. With that in mind, let’s walk through the process. You can follow along typing commands in either the console or the PowerShell ISE.
To make our PowerShell code re-usable, let’s define a variable for a folder we want to check.
$path = "c:\work"
You can verify by getting all files within that path.
dir $path -file -Recurse
We’re going to look at all files, but you could just as easily limit your search to files of a specific type or name. When approaching a PowerShell problem, it is helpful to look at a single, representative object to discover what you can use. I’m going to look at a single file.
PS C:\> get-item C:\work\ComputerData.xml | Select Name Name ---- ComputerData.xml
The name property is a string object that has a single property of length.
PS C:\> $file = get-item C:\work\ComputerData.xml PS C:\> $file.name ComputerData.xml PS C:\> $file.name.length 16
In PowerShell you can keep expanding properties like I’ve done here. But don’t confuse this property with the length property of the file itself, which is the file size. We can take the next step and use a custom property with Select-Object.
PS C:\> get-item C:\work\ComputerData.xml | Select Name,@{Name="NameLength";Expression={$_.name.length}} Name NameLength ---- ---------- ComputerData.xml 16
Now that we know how to get the value for one item, we can expand this and get it for all items. I’ll test using a subset of files.
dir $path -file -recurse | Select Name,@{Name="NameLength";Expression={$_.name.length}} -first 10
Obtaining a value for all items in Windows PowerShell. (Image Credit: Jeffery Hicks)
dir $path -file -recurse | Select Name,@{Name="NameLength";Expression={$_.name.length}} -first 10 | Sort NameLength -Descending
Sending objects to the pipeline to sort. (Image Credit: Jeffery Hicks)
dir $path -file -recurse | Select Directory,Name,@{Name="NameLength";Expression={$_.name.length}} | Sort NameLength -Descending | Select -first 10
Obtaining the top 10 longest file names. (Image Credit: Jeffery Hicks)
dir $path -file -recurse | Select FullName,@{Name="NameLength";Expression={$_.fullname.length}} | Sort NameLength -Descending | Select -first 10
Including the entire file path for longest file names in Windows PowerShell. (Image Credit: Jeffery Hicks)
PowerShell almost always has alternatives. This is a good thing because you might have different requirements for how you want to use this information. Another technique is to add a custom property to the file object with the Add-Member cmdlet.
dir $path -file -recurse | Add-Member -MemberType ScriptProperty -Name NameLength -value {$this.name.length} -PassThru -force | Sort NameLength -Descending | Select -first 10
Using the Add-Member cmdlet to add a custom property. (Image Credit: Jeffery Hicks)
pdir $path -file -recurse | Add-Member -MemberType ScriptProperty -Name NameLength -value {$this.name.length} -PassThru -force | Sort NameLength -Descending | Select -first 10 -property Directory,Name,NameLength,CreationTime,LastWriteTime
Using Add-Member in Windows PowerShell. (Image Credit: Jeffery Hicks)
dir $path -file -recurse | Add-Member -MemberType ScriptProperty -Name NameLength -value {$this.name.length} -PassThru -force | where {$_.NameLength -ge 80 } | Select fullname
Using Add-Member to filter a new, custom property in PowerShell. (Image Credit: Jeffery Hicks)
@{Expression={ some scriptblock }}
The scriptblock can use $_ to reference the current object in the pipeline. Here’s how we could apply this to the problem at hand.
dir $path -file -recurse | Sort @{Expression={$_.name.length}} -Descending | Select -first 10
Referencing the current object in the pipeline in PowerShell. (Image Credit: Jeffery Hicks)
More in PowerShell
Most popular on petri