Edit an object non-destructively in PowerShell - powershell

I am trying to format the resulting object without destroying it. But all my efforts and research has failed me. Any tips are welcome.
My code looks like this:
Set-Location 'C:\Temp'
$Files = Get-ChildItem -File | Select-Object FullName, Length
And what I get, is this:
FullName Length
-------- ------
C:\Temp\CleanupScript.txt 10600
C:\Temp\Columns.csv 4214
C:\Temp\Content.html 271034
C:\Temp\Content.txt 271034
C:\Temp\DirSizes.csv 78
What I want is this:
FullName Length
-------- ------
Temp\CleanupScript.txt 10600
Temp\Columns.csv 4214
Temp\Content.html 271034
Temp\Content.txt 271034
Temp\DirSizes.csv 78
When I tried this:
$Files = Get-ChildItem -File | Select-Object FullName, Length | % { $_.FullName.Remove(0, 3) }
I got the right result, but I lost the Length column.
PS C:\Temp> $Files
Temp\CleanupScript.txt
Temp\Columns.csv
Temp\Content.html
Temp\Content.txt
Temp\DirSizes.csv
Please help.
Big thanks
Patrik

The easiest way to do this is to construct the property you want in the Select command, such as:
$Files = Get-ChildItem -File | Select #{l='FullName';e={$_.FullName.Substring(3)}},Length
The format for this is a hashtable with two entries. The keys are lable (or name), and expression. You can shorten them to l (or n), and e. The label entry defines the name of the property you are constructing, and the expression defines the value.
If you want to retain all of the original methods and properties of the objects you should add a property to them rather than using calculated properties. You can do that with Add-Member as such:
$Files = GCI -File | %{Add-Member -inputobject $_ -notepropertyname 'ShortPath' -notepropertyvalue $_.FullName.Substring(3) -PassThru}
Then you can use that property by name like $Files | FT ShortPath,Length -Auto, while still retaining the ability to use the file's methods like Copy() and what not.

I would recommend using a calculated property and Split-Path -NoQualifier; e.g.:
Get-ChildItem -File | Select-Object `
#{Name = "NameNoQualifier"; Expression = {Split-Path $_.FullName -NoQualifier}},
Length
For help on calculated properties, see the help for Select-Object.
(Aside: To correct your terminology a bit, this is not modifying objects non-destructively but rather outputting new objects containing the properties you want formatted how you want them.)

Related

How can I replace a string inside a pipe?

I'm trying to replace some specific parts of a selected string but am only returning the length property. Here's my code:
Get-ChildItem "StartPath/Something/Files" -Recurse -File | Select "FullName | Foreach {$_.FullName -replace "StartPath",""} | Export-Csv "ResultPath.csv"
If I omit the foreach bit, this works in that it spits out the full path. I'd like to trim the full path as I'm iterating over tons of files. I'm trying to replace a bit of the path in the beginning of the string but my code above just spits out a CSV file with just string lengths.
Looks like:
"123"
"12"
"52"
and so forth.
The intended result would be a csv file with instead of:
StartPath/Something/Files1
StartPath/Something/Files2
I'd have
Something/Files1
Something/Files2
I've tried a number of things and can't seem to figure it out. Any help is appreciated.
If you pass a string to select / Select-Object (to its positionally implied -Property parameter), it must be a property name.[1]
If you want to perform open-ended operations and/or produce open-ended output for each input object, you must use the ForEach-Object cmdlet:
Get-ChildItem "StartPath/Something/Files" -Recurse -File |
ForEach-Object {
[pscustomobject] #{ FullName = $_.FullName -replace 'StartPath' }
} |
Export-Csv "ResultPath.csv"
Note the use of a [pscustomobject] wrapper that defines a FullName property, so that Export-Csv creates a CSV with that property as its (only) column.
If you pipe [string] instances directly to Export-Csv, their properties are serialized to the output file - and a [string]'s only (public) property is its length (.Length), which is what you saw.
[1] There's also a way to create properties dynamically, using so-called calculated properties, which are defined via hash tables.

Compare-Object different property names

Is there a way to use Compare-Object -property, for comparing 2 properties with different names? I have something like this:
$ComparisonProperty= $ComparisonProperty | Add-Member -MemberType AliasProperty -Name Url -Value ITEM_TARGET_URI -PassThru
Compare-Object $FirstFile $SecondFile -Property Url -PassThru | Where-Object{$_.SideIndicator -eq "<="} | Out-file .\result.txt
But this gives an error:
Cannot bind argument to parameter 'InputObject' because it is null
At $ComparisonProperty
//Edit
Sample data are 2 csv files with many headers, 1 with Url header in it, another with ITEM_TARGET_URI in it. Result should be strings from file 1 that do not exist in file 2. Comparison works if i provide them with the same property names, but the whole point is to force it to compare 2 properties with different names.
To answer the actual question calculated properties or property aliases would get you what you need. You have been trying to use the latter but you need to add the property to the file object itself. Consider the two file examples
id,first_name
1,Elia
2,Nikolos
3,Bert
4,Sharleen
5,Bill
id,beginning_name
1,Elia
2,Nikolos
3,Bert
4,Mildrid
5,Bill
Notice that the headers are different. So now lets try and create the property alias. Assume that I have already imported these files as CSV's
$file2 | Add-Member -MemberType AliasProperty -Name first_name -Value beginning_name
compare-object $file1 $file2 -Property first_name
That will give you the results you were expecting. I added an alias to the second file object
You could just work with column data
Another approach to this is to drop the properties and just work with string arrays. Since I know the headers I want I can just get those columns themselves as well
$namesfromfile1 = $file1 | Select-Object -ExpandProperty first_name
$namesfromfile2 = $file2 | Select-Object -ExpandProperty beginning_name
Compare-Object $urls1 $urls2
Depending on your PS version this can also be shortened. Whether or not its simplified is up to the reader.
Compare-Object ($file1).first_name ($file2).beginning_name

powershell filter to remove .pdf extension in the name of a file

I am trying to use powershell to get all child elements in a folder the code I am using is
Get-ChildItem -Recurse -path C:\clntfiles
this code gives output like
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 4/29/2015 9:11 AM 6919044 HD 100616 Dec2014.pdf
-a--- 5/1/2015 11:42 AM 7091019 HD 101642 Jan2015.pdf
I don't want Mode lastWriteTime Length and name of file without .pdf extension
the output should be like
Dec2014
Jan2015
I am not sure how to filter that. please advise
I'll start by posting something similar to Leptonator's answer, but simplified by using the Select-Object command (alias Select used in code because it's habit, and I'm lazy).
$files = Get-ChildItem -Recurse -path C:\clntfiles | Select -ExpandProperty BaseName
Now that gets you the file names without extension. But, you actually asked for only part of the file names, as the first file name is "HD 100616 Dec2014.pdf" and you specified that you actually only want "Dec2014" to be returned. We can do that a couple different ways, but my favorite of them would be a RegEx match (because RegEx is awesome, and I think the LastIndexOf/SubString combo is overly complicated imho).
So, a RegEx match of "\w+$" will get what you want. That is broken down like this:
\w means any letter or number
+ means 1 or more of them
$ means the end of the string/line
So that's 1 or more alpha-numeric characters at the end of the string. We pipe our array of file names into a ForEach-Object loop (alias ForEach used out of habit), and then we have:
$Files | ForEach{ [RegEx]::Matches($_,"\w+$")}
Now, this outputs a [System.Text.RegularExpressions.Match] object, which is more than you want, but it does have a property Value which is exactly what you asked for! So we use Select -Expand again for that property and the output is precisely what you asked for:
$files = Get-ChildItem -Recurse -path C:\clntfiles | Select -ExpandProperty BaseName
$files | ForEach{[regex]::Matches($_,"\w+$")} | Select -Expand Value
RegEx matches are really handy, and if you learn about them you can simplify that quite a bit more like this:
gci C:\clntfiles -Rec | ?{$_.BaseName -match "(\w+)$"} | %{$Matches[1]}
That one line, as well as the two line code above it both should output:
Dec2014
Jan2015
Something like this should do it for you..
$files = Get-ChildItem -Recurse -path C:\clntfiles
if ($files -ne $null)
{
foreach ($file in $files)
{
$file.BaseName
}
}
In my folder, it shows:
> 2014-03-28_exeresult_file
> 2014-03-30_exeresult_file
> 2014-03-31_exeresult_file
> 2014-04-02_exeresult_file
> 2014-04-03_exeresult_file
> 2014-04-04_exeresult_file
> 2014-04-06_exeresult_file
> 2014-04-08_exeresult_file
and are indeed .txt files
Hope this helps!
Use the following Get-ChildItem -Recurse -name -path C:\clntfiles. This will get you only the file names.
Working solution:
$names = Get-ChildItem -name
foreach($n in $names) {$n.Substring(0,$n.IndexOf("."))}
You can also use LastIndexOf if part of the file name is .

Getting a path and then converting it to a string?

$getusers = Get-ChildItem \\pc-name\c$\users\ | Select-Object Fullname
I'm running that line to get all the users who have logged into a pc.
Then I am checking every documents folder for files. I assumed it would be as simple as this:
foreach ($user in $getusers) {
Get-ChildItem "$user\documents"
}
but it seems that I have to convert the $getusers to string? Can someone help and explain what needs to be done? I think its simple I'm just not getting.
$dirs = Get-ChildItem \\pc-name\c$\users\ | Select-Object FullName | Where-Object {!($_.psiscontainer)} | foreach {$_.FullName}
This ended up working. I was able to figure it out.
In case someone else finds this in there search for help I wanted to add what I think the actual issue is. Consider the following line:
$getusers = Get-ChildItem \\pc-name\c$\users\ | Select-Object Fullname
That would return an object of fullnames.
FullName
--------
\\localhost\c$\users\jpilot
\\localhost\c$\users\matt
\\localhost\c$\users\misapps
\\localhost\c$\users\mm
The issue is that $getusers is a System.Object[] that has FullName NoteProperty and not a System.String[] as the loop would be expecting. What should be done in the following
$getusers = Get-ChildItem \\pc-name\c$\users\ | Select-Object -ExpandProperty Fullname
Now $getusers will contain an array of string
\\localhost\c$\users\jpilot
\\localhost\c$\users\matt
\\localhost\c$\users\misapps
\\localhost\c$\users\mm
That would make the rest of the script function as expected.

How can I reference the original object from values in an array?

I've done quite a bit of searching but can't seem to find an answer to this, but if it has been answered, I apologize, and just link me to it if you can.
What I'm trying to do is distribute files across 6 different paths based on which path currently has the least number of files in it.
What I thought to do is to add the responses from these ($Queue1-6 are just file paths) to an array and then sort them and get the path from the first object.
$QueueFiles1 = ( Get-ChildItem $Queue1 | Measure-Object ).Count
$QueueFiles2 = ( Get-ChildItem $Queue2 | Measure-Object ).Count
$QueueFiles3 = ( Get-ChildItem $Queue3 | Measure-Object ).Count
$QueueFiles4 = ( Get-ChildItem $Queue4 | Measure-Object ).Count
$QueueFiles5 = ( Get-ChildItem $Queue5 | Measure-Object ).Count
$QueueFiles6 = ( Get-ChildItem $Queue6 | Measure-Object ).Count
$FileNumArray = #($QueueFiles1, $QueueFiles2, $QueueFiles3, $QueueFiles4, $QueueFiles5, $QueueFiles6)
$FileNumArray = $FileNumArray | Sort-Object
The problem is (as far as I can tell) that after adding these values to the array, the object is lost and all that is left is the value, so now I don't know how to reference back to the original object to obtain the path information.
Any thoughts on how to do this would be appreciated and it doesn't need to be done with an array, like this, if there's an easier way to compare those file count values and obtain the path information of the lowest value.
Also, if there is more than 1 path with the lowest value, it doesn't matter which is returned.
Thanks in advance for any assistance.
Note that i have used some of my own folder to take place of the $queue(s). Also depending on the location of these your might be able to build a simple for each loop ie: if they were all subfolders of the same parent.
$Queue1 = "C:\Temp\NewName"
$Queue2 = "C:\temp\TaskManagement"
$Queue3 = "C:\temp\message_log.csv"
# Build a hashtable. Add each queue to the hash table.
$fileCount = #{}
# Set the queue as the name and the count as the value
$fileCount.Add("$Queue1", (Get-ChildItem $Queue1 | Measure-Object ).Count)
$fileCount.Add("$Queue2", (Get-ChildItem $Queue2 | Measure-Object ).Count)
$fileCount.Add("$Queue3", (Get-ChildItem $Queue4 | Measure-Object ).Count)
# Sort the results by the value of the hashtable (Counts from earlier) and select only the one.
$fileCount.GetEnumerator() | Sort-Object value | Select-Object -First 1 -ExpandProperty Name
Explaining the last line
.GetEnumerator() is required in order to sort the hashtable.
Sort-Object is Ascending by default so there is no need to mention it.
Select-Object -First 1 if you dont care which one you get as long as it has the smallest amount of files.
-ExpandProperty Name since you only really need the path and not the hashtable entry itself.