Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(edit)</p> <p>I figured out how to dynamically add a property (called a "script property") to the Fileobject, so now, I can use the syntax: $theFileObject.CompressedSize to read the size.</p> <p>(end of edit)</p> <p>Read Goyuix's response, and I thought "Cool, but isn't there some kind of type-extension capability in Powershell?". So then I found this Scott Hanselman post: <a href="http://www.hanselman.com/blog/MakingJunctionsReparsePointsVisibleInPowerShell.aspx" rel="nofollow noreferrer">http://www.hanselman.com/blog/MakingJunctionsReparsePointsVisibleInPowerShell.aspx</a> </p> <p>And I created a Script Property for the FileInfo object: CompressedSize.</p> <p>Here's what I did: (note: I'm quite new to Powershell, or at least I don't use it much. This could probably be made a lot better, but here's what I did:</p> <p>First, I compiled the Ntfs.ExtendedFileInfo from Goyuix's post. I put the DLL in my Powershell profile directory (Documents\WindowsPowershell)</p> <p>Next, I created a file in my profile directory named My.Types.ps1xml.</p> <p>I put the following XML into the file:</p> <pre><code>&lt;Types&gt; &lt;Type&gt; &lt;Name&gt;System.IO.FileInfo&lt;/Name&gt; &lt;Members&gt; &lt;ScriptProperty&gt; &lt;Name&gt;CompressedSize&lt;/Name&gt; &lt;GetScriptBlock&gt; [Ntfs.ExtendedFileInfo]::GetCompressedFileSize($this.FullName) &lt;/GetScriptBlock&gt; &lt;/ScriptProperty&gt; &lt;/Members&gt; &lt;/Type&gt; &lt;/Types&gt; </code></pre> <p>That code (once merged into the type system) will dynamically add a property named CompressedSize to the FileInfo objects that are returned by get-childitem/dir. But Powershell doesn't know about the code yet, and it doesn't know about my DLL yet. We handle that in the next step:</p> <p>Edit Profile.ps1. in the same directory. Now, my Profile file happens to already have some stuff in it because I have the Community Extensions for powershell installed. Hopefully, I'm including everything that you need in this next code snippet, so that it will work even on a machine that does not have the extensions. Add the following code to Profile.ps1:</p> <pre><code>#This will load the ExtendedfileInfo assembly to enable the GetCompressedFileSize method. this method is used by the #PSCompressedSize Script Property attached to the FileInfo object. $null = [System.Reflection.Assembly]::LoadFile("$ProfileDir\ntfs.extendedfileinfo.dll") #merge in my extended types $profileTypes = $ProfileDir | join-path -childpath "My.Types.ps1xml" Update-TypeData $profileTypes </code></pre> <p>Now, the $ProfileDir variable that I reference is defined earlier in my Profile.ps1 script. Just in case it's not in yours, here is the definition:</p> <pre><code>$ProfileDir = split-path $MyInvocation.MyCommand.Path -Parent </code></pre> <p>That's it. The next time that you run Powershell, you can access the CompressedSize property on the FileInfo object just as though it is any other property. Example:</p> <p>$myFile = dir c:\temp\myfile.txt </p> <p>$myFile.CompressedSize</p> <p>This works (on my machine, anyway), but I would love to hear whether it fits in with best practices. One thing I know I'm doing wrong: in the Profile.ps1 file, I return the results of LoadFile into a variable that I'm not going to use ($null = blah blah). I did that to suppress the display of result of load file to the console. There is probably a better way to do it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload