Odd PowerShell behavior and Add-Member - powershell

Can someone help me understand why the $_ works differently for the Add-Member function than it seems to for other PowerShell functions? There must be some nuance that I am missing here.
Using the sample CSV file below, the first 3 examples work fine. The fourth, which seems pretty straightforward, does not. The error Powershell returns is:
The variable '$_' cannot be retrieved because it has not been set.
Is this "normal" or have I miscoded something?
Thanks in advance.
Test.csv
Column1,Column2, Column3
Rock,1,abc
Paper,2,efg
Scissors,3,hij
$obj = Import-CSV "C:\test.csv"
(1) $obj | Add-Member -MemberType NoteProperty -Name IdNumber -value "ROCK" -PassThru| Format-Table
(2) $obj | ForEach-Object { $_ | Add-Member -MemberType NoteProperty -Name IdNumber -value $_.Column1 ; $_} | Format-Table
(3) $obj | ForEach-Object { Add-Member -InputObject $_ -MemberType NoteProperty -Name IdNumber -value $_.Column1 ; $_ } | Format-Table
(4) $obj | Add-Member -MemberType NoteProperty -Name IdNumber -value $_.Column1 -PassThru| Format-Table

When you do this:
$obj | Add-Member -MemberType NoteProperty -Name IdNumber -value $_.Column1 -PassThru
You are accessing $_ outside of a pipeline bound context e.g. inside a foreach-object expression or within a scriptblock value specified for a pipeline bound parameter. The Value parameter is not pipeline bound. If it were you would be able to do this:
$obj | Add-Member -MemberType NoteProperty -Name IdNumber -value {$_.Column1} -PassThru
As it stands, the easiest way to do what you want is:
$obj | ForEach-Object { Add-Member -Inp $_ NoteProperty -Name IdNumber -Value $_.Column1 -PassThru } | Format-Table

Related

How to export file properties in csv using powershell

I wanted to export a csv-File with File-Properties from some tif-Files.
With this command
Get-ChildItem -Recurse C:\tifs\ |
ForEach-Object {$_ | add-member -name "Owner" -membertype noteproperty `
-value (get-acl $_.fullname).owner -passthru} | Sort-Object fullname |
Select FullName,CreationTime,LastWriteTime,Length,Dimensions |
Export-Csv -Force -NoTypeInformation C:\Test\export.csv
I can export a csv just fine. But as soon as I want to add properties like vertical resolution it fails. I don't quite understand why.
In order to get to the "extended" file properties (like Dimension and Resolution metadata) you have to resort to using the Windows Visual Basic shell options from inside PowerShell as Steven helpfully pointed out. Here is a code sample that should give you the result:
$files = #()
$folder = (New-Object -ComObject Shell.Application).namespace("C:\tifs")
# Loop through each file in folder
foreach ($f in $folder.Items()) {
$a = 0
# Print all the available properties (for debugging purposes)
for ($a ; $a -le 266; $a++) {
if($folder.GetDetailsOf($f, $a)) {
Write-Host "Property: $($folder.GetDetailsOf($folder.items, $a))"
Write-Host "Value: $($folder.GetDetailsOf($f, $a))"
Write-Host "Index: $($a)"
}
}
# Store data in custom PowerShell object
$obj = New-Object -TypeName PSOBJECT
# Fill each property with the file metadata (by index number)
$obj | Add-Member -MemberType NoteProperty -Name FullName -Value $folder.GetDetailsOf($f, 194)
$obj | Add-Member -MemberType NoteProperty -Name CreationTime -Value $folder.GetDetailsOf($f, 4)
$obj | Add-Member -MemberType NoteProperty -Name LastWriteTime -Value $folder.GetDetailsOf($f, 5)
$obj | Add-Member -MemberType NoteProperty -Name Length -Value $folder.GetDetailsOf($f, 1)
$obj | Add-Member -MemberType NoteProperty -Name Dimensions -Value $folder.GetDetailsOf($f, 31)
# Add custom object to a collection
$files += $obj
}
# Export collection to CSV
$files | Export-Csv -Force C:\Test\export.csv -NoTypeInformation -Encoding UTF8
From what I can tell there's no obvious PowerShell/.Net approach to getting additional file meta data. However. there are some COM based approaches.
Check Scripting Guys
And they reference this
You will still have to correlate the data. I usually do that by building hash table keyed of same values, in you can index the metadata using the path property, then use the FullName property of the file info objects to reference it, so you can the properties.

Is there a way to get-content from a specific NoteProperty in Powershell?

So I have created 2 objects called $Row1 and $Row2 which are populated with values in the following manner
$Row1 | Add-Member -MemberType NoteProperty -Name Name -Value $name
$Row1 | Add-Member -MemberType NoteProperty -Name Age -Value $age
$Row2 | Add-Member -MemberType NoteProperty -Name Name -Value $name
$Row2 | Add-Member -MemberType NoteProperty -Name Age -Value $age
I'm now looking for a way to access only the age value with Get-Content.
The answer for this is that it would be suffice with writing $Row1.(TheNotePropertyName you want to access)
eg.
$Row1.Age
$Row1.Name
$Row2.Name
This works both for PScustomobjects and Psobjects. The difference between these are found here.

Powershell list recursively files attributes to csv

I am trying to fill a csv file with all the attributes of the files contained within a folder. I have troubles recovering the fileversion & assembly
$arr = #()
gci C:\Temp -recurse | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Size (Get-Item $_.Length/1MB)
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$obj | Add-Member NoteProperty LastAccess $_.LastAccessTime
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Creation $_.CreationTime
$obj | Add-Member NoteProperty LastWrite $_.LastWriteTime
$obj | Add-Member NoteProperty ReadOnly $_.IsReadOnly
$obj | Add-Member NoteProperty FullName $_.FullName
$obj | Add-Member NoteProperty Date (Get-Date -format "yyyy-MM-d HH:mm")
$obj | Add-Member NoteProperty Version ($_.FileVersion)
$obj | Add-Member NoteProperty Assembly ($_.AssemblyVersion)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\temp\File\report.csv"
try this
$arr = #()
gci C:\Temp -recurse -File -Filter *.dll | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Size (Get-Item $_.Length/1MB)
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$obj | Add-Member NoteProperty LastAccess $_.LastAccessTime
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Creation $_.CreationTime
$obj | Add-Member NoteProperty LastWrite $_.LastWriteTime
$obj | Add-Member NoteProperty ReadOnly $_.IsReadOnly
$obj | Add-Member NoteProperty FullName $_.FullName
$obj | Add-Member NoteProperty Date (Get-Date -format "yyyy-MM-d HH:mm")
$obj | Add-Member NoteProperty Version ($_.VersionInfo.FileVersion)
$obj | Add-Member NoteProperty Assembly ([Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\temp\File\report.csv"
you can simplify your code like this
gci C:\Temp -recurse -File -Filter *.dll | % {
New-Object PSObject -Property #{
Directory= $_.DirectoryName
Name= $_.Name
Size= $_.Length/1MB
Owner= ((Get-ACL $_.FullName).Owner)
LastAccess= $_.LastAccessTime
Extension= $_.Extension
Creation= $_.CreationTime
LastWrite= $_.LastWriteTime
ReadOnly= $_.IsReadOnly
FullName= $_.FullName
Date= (Get-Date -format "yyyy-MM-d HH:mm")
Version= ($_.VersionInfo.FileVersion)
Assembly= ([Reflection.AssemblyName]::GetAssemblyName($_.FullName).Version)
}
} | Export-CSV -notypeinformation "c:\temp\File\report.csv"
1)The issue what you are facing is because of permission. Try running the script in elevated mode(run as administrator).
2)You should not try to save anything under C:\temp folder. Better to create a folder in D:\ or E:\ and put it over there as D:\temp_dump\report.csv
3)if you are putting get-item on the size, then that is not a valid one since you have to pick whatever inside the folder.
Below Script is working fine.
##########################################################
$arr = #()
$Folder_path="E:\PS"
gci $Folder_path | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Size (Get-ChildItem $Folder_path | Measure-Object -property length -sum)
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$obj | Add-Member NoteProperty LastAccess $_.LastAccessTime
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Creation $_.CreationTime
$obj | Add-Member NoteProperty LastWrite $_.LastWriteTime
$obj | Add-Member NoteProperty ReadOnly $_.IsReadOnly
$obj | Add-Member NoteProperty FullName $_.FullName
$obj | Add-Member NoteProperty Date (Get-Date -format "yyyy-MM-d HH:mm")
$obj | Add-Member NoteProperty Version ($_.FileVersion)
$obj | Add-Member NoteProperty Assembly ($_.AssemblyVersion)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "E:\report.csv"
#
In order to get fileversion you have to use this:
$obj | Add-Member NoteProperty Version ($_.VersionInfo.FileVersion)
You cannot get AssemblyVersion from VersionInfo

Powershell: File properties - Bytes to Mb

Currently have the following script that outputs some file data to a report. The file length is in bytes though and wondering how I can convert that to MB before outputting to the array.
$arr = #()
gci C:\stuff -recurse | ? {$_.PSIsContainer -eq $False} | % {
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Directory $_.DirectoryName
$obj | Add-Member NoteProperty Name $_.Name
$obj | Add-Member NoteProperty Length $_.Length
$obj | Add-Member NoteProperty created $_.creationtime
$obj | Add-Member NoteProperty Access $_.LastAccessTime
$obj | Add-Member NoteProperty LastWritten $_.LastWriteTime
$obj | Add-Member NoteProperty Extension $_.Extension
$obj | Add-Member NoteProperty Owner ((Get-ACL $_.FullName).Owner)
$arr += $obj
}
$arr | Export-CSV -notypeinformation "c:\files.csv"
When converting into MB, just put brackets around it:
$obj | Add-Member NoteProperty Length ($_.Length/1MB)
or maybe more useful:
$obj | Add-Member NoteProperty MB ("{0:N3}" -f ($_.Length/1MB))
to only show the first three digits after the point.

PowerShell to fetch installed programs

I will be hosting a file on a remote server (read-only) and asking people to run the file on their machines to gather installed program information. I want the file to be saved to their Desktop in their user space, so that I can then have them send it to us.
I have the script, but I'm not managing to obtain information from both "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", and "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" in the same output file. I'm obviously missing something inherently obvious, as PowerShell is clearly able to do this, and I'm asking that someone please save me from my PEBKAC issue!
Thank you in advance, appreciated!
Here is my code;
$computers = "$env:computername"
$array = #()
foreach($pc in $computers){
$computername=$pc
$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
$UninstallKey="Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$reg=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computername)
$regkey=$reg.OpenSubKey($UninstallKey)
$subkeys=$regkey.GetSubKeyNames()
Write-Host "$computername"
foreach($key in $subkeys){
$thisKey=$UninstallKey+"\\"+$key
$thisSubKey=$reg.OpenSubKey($thisKey)
$obj = New-Object PSObject
$obj | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $computername
$obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $($thisSubKey.GetValue("DisplayName"))
$obj | Add-Member -MemberType NoteProperty -Name "DisplayVersion" -Value $($thisSubKey.GetValue("DisplayVersion"))
$obj | Add-Member -MemberType NoteProperty -Name "InstallLocation" -Value $($thisSubKey.GetValue("InstallLocation"))
$obj | Add-Member -MemberType NoteProperty -Name "Publisher" -Value $($thisSubKey.GetValue("Publisher"))
$array += $obj
}
}
$array | Where-Object { $_.DisplayName } | select ComputerName, DisplayName, DisplayVersion, Publisher | export-csv C:\Users\$env:username\Desktop\Installed_Apps.csv
Right now the following two lines set the same variable:
$UninstallKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
$UninstallKey="Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
Use this:
$UninstallKey = #(
'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
)
Then wrap the real logic in:
$UninstallKey | ForEach-Object {
$regkey=$reg.OpenSubKey($_)
# the rest of your logic here
}