Answered by:

PowerShell: Disk Space List

Question

I'm new to PowerShell and struggling to find a way forward with using the win32_logicaldisk class within my basic script.

I have found that when I set the properties for my new object below for the win32_logicaldisk class, I will get multiple results for each property specified which is expected, and the Operating System and Computer System classes work fine as there is only
one result returned

What I am trying to achieve is a result for each computer to list all available drives, their total size, free space and percentage of free space.

I don't want to filter out the device ID when getting info from the win32_logicaldisk class as all device ID's could be any letter, so I would like them all to be identified, then used for when I name my properties.

The end result would be something like this (values entered are just for the example):

C: Drive Total Size : 600.00 GB

C: Drive Free Space : 300.00 GB

C: Drive Percentage Free : 50.00%

R: Drive Total Size : 600.00 GB

R: Drive Free Space : 300.00 GB

R: Drive Percentage Free : 50.00%

I am assuming I need to run another Foreach construct for the disks and I would need to use a variable for each device ID found to use in the naming of my properties, but just can't get my head around it.

All replies

I have gotten a little further with this (see below script), however what I am finding now is it only selecting the last disk found in the foreach construct and errors out on all the disks found prior.

Thanks for your response jrv, I am going for the code to store multiple disks.

From your response I am still unsure which direction to take?

I've minimised the script to just contain the relevant disk data and I still have the same issue where it only picks up the last drive - For example, on my local computer I should be seeing output for the drives c: d: f: and h: and I am only getting the
results for h:

That is not the script I posted. Please try to understand that what you are doing is wrong. I am trying to get you going in the right direstion.

I am sorry that I am new at PowerShell. I have only been using it for 8 years and you guy are so much better. If you think your way is correct then you do not need to ask for our help.

I suggest that you look closely at what I posted and the issues I raised. If you do not understand what I am referring to then ask a specific question.

What you are trying for cannot be done the way you are approaching it. It is an issue of one-to-many relations. We need to de-normalize the data due to this relationship.

If you do it your way you will end up with broken objects. If you are just trying for a grouped report then use grouping to get your report.

whatever $computerlist| ft -group computername -auto

You can also write a custom report formatter to get your format.

Rules: collect data into objects, select properties, format output.

That is the way of PowerShell and all object programming. Once you learn how to structure a program and how to get the components of PowerShell to work correctly these things will become easy. By trying to cobble your way through this without seeing
what is happening you will become frustrated.

Look at your original script. You are enumerating disks and assigning things arbitrarily to an object. In objects the output will be restricted to the components of the first object in the collection. If you add arbitrary properties along
the way they will become somewhat inaccessible. The de-normalized view gives you everything like a SQL table and you can then sue output query methods to arrange it in a report.

Of course if you just want text to look at the just use Write-Host to output the exact text you want and forget about objects.

Now try and understand why I chaged that the way I did. Try and see what you were doing, why it was unnecessary and what was wrong.

Hint: You cannot change the name of fields in an object dynamically and have the objects behave consistently. That is OOP 101.

You can build a hash dynamically but you cannot use a dynamic hash to build objects. The has used for creating an object must be identical every time you generate an object to the pipeline or only the first object will be used as a property builder.
(Posh 101)

Sorry if I am coming across as ignoring what you are trying to help me with - as I mentioned in my OP, I am very new to PowerShell and my understanding is very well below yours...believe me.

What I am trying to achieve is to create a function, that creates a new object, that has a number of custom defined properties that can all be seen when you run get-member on the function itself. This will then allow anyone to format the data however
they would like by running the function and using select-object and their own custom expressions.

Using your example, my desired result would be all of the below to be NoteProperty member types:

Sorry if I am coming across as ignoring what you are trying to help me with - as I mentioned in my OP, I am very new to PowerShell and my understanding is very well below yours...believe me.

What I am trying to achieve is to create a function, that creates a new object, that has a number of custom defined properties that can all be seen when you run get-member on the function itself. This will then allow anyone to format the data however
they would like by running the function and using select-object and their own custom expressions.

Using your example, my desired result would be all of the below to be NoteProperty member types:

Do I need to create separate objects in the one function, and then link them together somehow to make a single object?

Again - you cannot do what you are trying to do. It is not possible to build objects in the pipeline the way you are trying to do it. Please read my earlier posts very carefully. GO back and spend some more time with the basics of the PowerShell
pipeline.

In OOP two objects that have different properties are not the same class but are of two different classes.

Microsoft is conducting an online survey to understand your opinion of the Technet Web site. If you choose to participate, the online survey will be presented to you when you leave the Technet Web site.