Use an intermediate calculated property:
Get-ChildItem -Path *.txt* -Recurse | Select-Object *, @{ n='a'; e={ $_.Name.Length } } |
Format-Table Name, a
Note:
This uses a property on the output object rather than a variable to carry the value of interest.
In doing so, it changes the type of the objects passed through the pipeline to type [pscustomobject] - a custom type with the added a property. This may or may not be a problem (not a problem with piping to Format-Table).
By contrast, if your plan is to simply process the input objects in a ForEach-Object process block anyway, you can simply define a variable inside that block:
Get-ChildItem -Path *.txt* -Recurse | ForEach-Object {
$a=$_.Name; $a.Substring(0, $a.IndexOf('.txt'))
}
(As an aside: This example command doesn't strictly need a helper variable, because it can more succinctly expressed as:
Get-ChildItem -Path *.txt* -Recurse | ForEach-Object { $_.Name -replace '\.txt.*$' }).