# PowerShell cmdlet to list the System files in the root of C:\
$i=0
$GciFiles = get-ChildItem "c:" -force |where {$_.attributes -match "System"}
foreach ($file in $GciFiles) {$i++}
$GciFiles |sort |ft name, attributes -auto
Write-host "Number of files: " $i
if
IF
One of my friends needed a script that would be able to list all files in a folder, and then he should be able to choose the file, and return the name of the file. Quite similar to what I have written about in a previous post. But in the previous example I used the BrowseForFolder method, which works in a very limited way, when you try to choose files with it.. (I guess there is a reason why it is called BrowseForFOLDER)
So I tried to figure out another way to do it.. And I came across a script Marc Van Orsouw wrote, and that gave me an idea..
Use the .Net framework and create a function, that shows all the files in a folder in a Listbox, then use and event trigger to see when the “file” is clicked.
The script is using PsEventing, which you can download from CodePlex… (Eventing should be included in PowerShell CTP 2 -v2, but I have not played with that yet, so you need to download PsEventing )
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
Add-PSSnapin PSEventing
Function invoke-TextViewer ($dir = '.'){
$form = New-Object System.Windows.Forms.Form
$form.text = "PowerShell Text viewer " + (resolve-path $dir)
$lv = New-Object System.Windows.Forms.ListView
$lv.View = 'list'
$lv.Dock = 'Fill'
$form.Controls.Add($lv)
$Form.Add_Shown({
ls $dir |? {$_.PSIsContainer -eq $false} |% {$lv.Items.Add($_.name)}
$form.Activate()
})
$form.show()
Connect-Eventlistener -VariableName lv -EventName ItemActivate
Connect-Eventlistener -VariableName form -EventName Closed
while( -not $path) {
$event = get-Event -Wait
if ($event.Name -eq "Closed") {return}
$path = join-path $dir $event.source.value.selecteditems[0].text
Return $path
}
}
$Filename = invoke-TextViewer c:\scripts
$Filename
As you can see, I have created a function that takes a “Path” as an input, it will then read all the files in the given directory, and populate a listbox with it. You can then select one of the files, in the listbox, then the function will return the full path to the file.
I am testing a new Syntax Highlighter to make made code posts look better…
The new one looks really good, and has some nice features like “copy to clipboard” and “view plain”, but unfortunately it does not support PowerShell…
So if you see my code snippets change appearance, you now know why
I had to run a script the other day, that I wrote some time ago to change some files in a folder, the script uses the Shell.Application COM object BrowseForFolder method, to show a GUI that lets you browse through the directory structure and choose a folder. I thought that it might come in handy one day, to have this functionality in PowerShell as well.
First off I remembered seeing someone creating a PowerShell script, that read the input from WSH’s InputBox() by using the COM object MSScriptControl.ScriptControl.
$a = new-object -comobject MSScriptControl.ScriptControl
$a.language = "vbscript"
$a.addcode("function getInput() getInput = inputbox(`"Message box prompt`",`"Message Box Title`") end function" )
$b = $a.eval("getInput")
So I thought that if you can do the simple script above, you can do something a little more advanced.
I started out by looking at the example I had allready created, just copied that to the $a.addcode line of code, that didn’t work of course. After playing with it for a while, I figured out that it was problems with my ‘ ” ”s, they had to be escaped in order for the code to be evaluated correctly, so I ended up with something like this.
Function Get-Folder {
$a = New-Object -com MSScriptControl.ScriptControl
$a.language = "vbscript"
$a.addcode("Function ShowBrowse() `r Set objShell = CreateObject(`"Shell.Application`") `r Set objFolder = objShell.BrowseForFolder (0, `"Choose a Folder:`", 0, `"`") `r If objFolder Is Nothing Then `r WScript.Quit `r End If `r Set objFolderItem = objFolder.Self `r ShowBrowse = objFolderItem.Path `r End Function" )
$b = $a.eval("ShowBrowse")
Return $b
}
The above has been incapsulated into a function for ease of use.
I posted the above script on the Minasi forum, and only minutes went by, before I had the first reply.. It was from no other than Don Jones himself, asking if I knew I could use COM objects directly in PowerShell, I thought to my self, Yes, I have just called one in the script above.
Then it dawned on me, I used PowerShells functionality to create a “MSScriptControl.ScriptControl” object, that then creates a “Shell.Application” object in WSH “MSScriptControl.ScriptControl” runspace. Why not create the “Shell.Application” object directly.
That gave me a lot simpler code, and a chance to slap myself for not thinking of that earlier, here is what it looks like
Function Get-folder {
$a = New-Object -ComObject "Shell.application"
$b = $a.BrowseForFolder(0,"Choose Folder",0,"")
$c = $b.Self
$c.path
}

Categories
Tag Cloud
Blog RSS
Comments RSS
Last 50 Posts
Back
Void « Default
Life
Earth
Wind
Water
Fire
Light 