Windows PowerShell Week Question and Answer Log

Wednesday, November 8: Objects, Objects Everywhere

Following is an edited version of the question-and-answer log compiled during the Windows PowerShell Week webcast Objects, Objects Everywhere: Working With Objects in Windows PowerShell. For more information about Windows PowerShell Week 3 – including the Q-and-A logs for the other webcasts – please visit the Windows PowerShell Week home page.

*

Objects, Objects Everywhere: Working With Objects in Windows PowerShell


feedback.gifspacer.gifWill the presentation be available offline for future reference?

Yes, it will be posted to www.microsoft.com/webcasts as well as http://www.microsoft.com/technet/scriptcenter/webcasts/ps.mspx.


feedback.gifspacer.gifOk, part 2: Got a link for how to create a HKCR drive as a PSProvider? :)

Try this:

new-psdrive hkcr -psprovider registry -root hkcr

You can then use set-location hkcr:\ to get to the registry hive.


feedback.gifspacer.gifI have a Perl script that uses browser code to automatically post data to a form in a Web page. I would like to do this with VBScript but have had difficultly finding a COM object in Internet Explorer or Windows that can do this. Is there a PowerShell class that could do this? How would I search for such a class other than going through the registry line-by-line?

PowerShell can access Web Forms through the .NET Framework. You could search the PowerShell docs and the .NET Framework docs.


feedback.gifspacer.gifI need a script that enumerates all the files on a server share. I have Power Shell and .NET 2.0 loaded on my machine. Is there a script that I could "tweak" to make that work? All the files end in *.log.

You could probably modify the script used in this Hey, Scripting Guy! to do that: http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug06/hey0830.mspx.


feedback.gifspacer.gifDoes the PowerShell team have a Coding Convention page or will the Scripting Guys take the lead and set a de facto standard for us?

See the Scripting with PowerShell link on the Script Center home page. I believe the Scripting Guys have set a standard in conjunction with the PowerShell team.


feedback.gifspacer.gifThese Q-and-As contain a lot of info. Can I download the ones from the previous webcasts somewhere? If not, please make them available! :)

They're available here: http://www.microsoft.com/technet/scriptcenter/webcasts/ps.mspx.


feedback.gifspacer.gifAny plans for adding IntelliSense to retrieve methods, properties, etc.? Are there third-party apps app that do this?

I know that PrimalScript 4.1 has this capability. A number of copies of this software are being given away as part of the PowerShell Scripting Sweepstakes: http://www.microsoft.com/technet/scriptcenter/topics/winpsh/contest/default.mspx.


feedback.gifspacer.gifDoes PowerShell include a Cmdlet for fast sorting or arrays?

Sure. Try this:

$a = 4,3,5,2,1; $a | sort

Simplistic, but you get the idea.


feedback.gifspacer.gifCan you access an object that wasn't instantiated by PowerShell?

Any COM or .NET object needs to be instantiated before you can use it.


feedback.gifspacer.gifWhen instantiating objects, how can we browse or know what properties or methods available?

WMI and .NET Framework use reflection to reveal their objects, properties and methods. For WMI, see the Scriptomatic tool code for information on reflecting WMI objects. For the .NET Framework, Visual Studio and the SDK will give you Intellisense and lots of documentation.


feedback.gifspacer.gifWill PowerShell run on Windows 2000 server?

No. Right now it's restricted to XP and Windows 2003. We don't know whether it will be backported to Windows 2000 or not.


feedback.gifspacer.gifCan I access information from remote machines?

PowerShell does not have built-in remoting. However, you can use the remoting provided by WMI, other COM libraries, and .NET Framework classes.


feedback.gifspacer.gifEven though it's not technically supported, will PowerShell run on Vista, or will we need to wait for a Vista-specific version?

You'll have to wait for the Vista-specific version (which should be out fairly soon). The current build will not install on Vista; I think it tells you that you don't have enough available disk space, regardless of how much disk space you actually have.


feedback.gifspacer.gifIs there a line break character in PowerShell like using & _ in VBScript so you can split up long lines of commands?

Yes, ` (the back-tick or accent grave character) is used just like _ in VBS.


feedback.gifspacer.gifIs there any hope for PowerShell being backported to Win2000? Are political or technical problems preventing this?

Don't know for sure. My guess is this is probably a combination of resource issues and technical issues. But that's just a guess.


feedback.gifspacer.gifCan you access an instance of a running process via its PID and manipulate it from PowerShell even if it was not instantiated from within PowerShell?

You can access it and do things like terminate that process. However, if it's an instance of Excel you can't connect to it and start adding data to the spreadsheet.


feedback.gifspacer.gifHow do I get the result of $obj | measure-object into a variable?

Try this:

$yourvar = $obj | measure-object


feedback.gifspacer.gifCan a PowerShell script be run on a machine that doesn’t have PowerShell installed, like you would run wscript.exe <script>?

No, you have to install PowerShell before you can use it. It's not included in any OS so far.


feedback.gifspacer.gifWhen running the $objExcel.Quit() command, Excel closes, but the process is still open in the background. How do you kill the process also?

To be honest, we didn't know that was a problem. However, you can stop all instances of Excel by using this command:

Stop-Process -processname excel

Or, if you know the process ID, use this command:

Stop-Process 3512


feedback.gifspacer.gifIn the Excel example the last command was $objExcel.Quit. Should this have closed the spreadsheet? I got a list of member type, value, name etc

You need to follow Quit with parentheses:

$objexcel.quit()


feedback.gifspacer.gifHow can I save a copy of yesterday's (Day 2) presentation to PDF?

Unfortunately you can't print a PDF from a replay file. However, you should receive a follow up email containing a link to the slides.


feedback.gifspacer.gifAn earlier question about sort functionality made me wonder about alphabetizing and ordering by date. Sorry if this has already been addressed.

You can sort by multiple properties and you can specify descending or ascending order for each item. Best of all, because it works with objects and properties PowerShell knows to treat dates as dates, numbers as numbers, etc. See this article for details: http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/sort-object.mspx.


feedback.gifspacer.gif$foo = Get-WmiObject | Measure-Object asks me to "supply value for class."

Try this:

$foo = Get-WmiObject Win32_Process | Measure-Object

Get-WmiObject doesn't return an object unless you tell it what kind of object to return.


feedback.gifspacer.gifWill there be a way to restore the default settings after modifying properties?

It's up to you to keep track of what you've changed and change them back, although you have the history of PowerShell commands you can review.


feedback.gifspacer.gifIt seems there are assumptions that we know a lot of the background already ... and I don't.

Sorry. This is day 3 of a 5-day series, so, yes, it does assume some prior knowledge. The archive version of day 1's webcast is archived at http://www.microsoft.com/technet/scriptcenter/webcasts/ps.mspx, and day 2 will be available there in another day or two.


feedback.gifspacer.gifWhy are there Export-* and ConvertTo-* Cmdlets? It seems that these are synonymous verbs.

Not sure, to be honest. There's likely a technical reason for this, but I'm not sure what it would be.


feedback.gifspacer.gifThere is a lot of typing involved. Maybe I missed it, hope I'm not being dumb here (I don't have the PowerShell available to test with, just observing). However, when you have initialized an object such as $obj, when you press the dot, does it give you a list of the methods/properties, like it does in the Visual Studio environment?

No, there's no Intellisense in PowerShell. It's just a command shell, not an IDE.


feedback.gifspacer.gifDay 2 is available: https://www119.livemeeting.com/cc/mseventsbmo/view?id=1032313500&role=attend&pw=CCC8C451.

Thanks


feedback.gifspacer.gifAs I'm going through the demos here, I'm noticing that tab-completion is sometimes VERY slow; other times it's instant. Any idea why this is?

It kind of depends on where you are and what you're doing. For example, suppose you're in a directory with thousands of files and you're using TAB completion for a file name. PowerShell has to load up and check all of those file names in order to do TAB completion. TAB completion for Cmdlets is pretty fast because there are only 120-some Cmdlets to deal with.


feedback.gifspacer.gifIf a hacker were to exploit this the machine would have to be reloaded.

Not sure what this refers to, but all scripting is very powerful. The stuff we're talking about is designed to be done by sys admins in a secure environment. But it can't do anything that your credentials can't do in the GUI.


feedback.gifspacer.gifPart II of the dumb questions: can the CTRL-V work for pasting in text, since Intellisense isn't available? Or will the PowerShell environment behave like the CMD prompt? Right-click to paste?

Like Cmd.exe: right-click to paste.


feedback.gifspacer.gifI want to how many process are running, the count.

Try this:

get-process | measure-object


feedback.gifspacer.gifTruthfully, I am comfortable with PowerShell and Cmdlets, but I don't really get WMI objects and the things you are doing with them. I'm with the person above who lamented the assumed knowledge. Maybe next time you could mention that WMI is a prerequisite.

For better or worse WMI is crucial to being able to do system admin tasks in PowerShell. If you're new to WMI you might want to take a look at the webcast WMI is Not a Four-Letter Word, which is available from here: http://go.microsoft.com/fwlink/?LinkId=30793.


feedback.gifspacer.gifThe guy that wanted to manage file security with VBScript should look at Xcacls.vbs: http://www.microsoft.com/downloads/details.aspx?FamilyID=0ad33a24-0616-473c-b103-c35bc2820bda&displaylang=en.

Xcacls is good. There are a few areas where scripting is thin and existing command-line tools are well-developed, and this is one.


feedback.gifspacer.gifAfter creating a new object, how can I list the properties of the new object?

Just pipe it to Get-Member, like so:

new-object -com scripting.filesystemobject | get-member


feedback.gifspacer.gifI want to save the result of get-process | measure-object in a variable. I tried $count = get-process | measure-object but I get an error.

That should work; I just tried it on my machine and the correct info got stored in $count. What error do you get?


feedback.gifspacer.gifCan you overload a constructor?

I don't think you can, although we haven't explored this yet. PowerShell is still a scripting language, not a .NET Framework language; I think you need C#, VB.NET etc. to overload.


feedback.gifspacer.gifHow can I assign a value to a variable and use it instead of $pshome?

You mean something like this:

$a = "c:\windows"; cd $a


feedback.gifspacer.gifGet-process | tee-object $count | measure-object.

Thanks.


feedback.gifspacer.gifYes, but I want to issue commands against it, not cd.

This works:

get-childitem $a

Do you have an example of what you want to do?


feedback.gifspacer.gif ($Count = get-process) | measure-object

That will store all the process information in $count. If that's what you want to do then that works great.


feedback.gifspacer.gifKids are easy to blame for almost anything ... I know, I do!

In a few years, Dean will have them trained to do routine coding tasks.


feedback.gifspacer.gifI love parentheses, too -- old habit. Can you write, for example, New-Object(System.Windows.Forms.Form)? What if you need to add some options?

No, I like parentheses too, but PowerShell Cmdlets are different from functions or methods. If you want to use that kind of function, try VBS, JS, C#, etc.


feedback.gifspacer.gifLoadWithPartialName reminded me of wildcards. Does PowerShell support them?

Yes, * and ? in quite a few contexts. And regular expressions.


feedback.gifspacer.gifCan you give the historical reason .MSH was renamed to .PS1 ?

It's not so much historical as legal, marketing, maybe a few focus groups. Naming products (and their file extensions) gets complicated.


feedback.gifspacer.gifTHAT's the ticket! Then the kids can support him!

The family that codes together stays together.


feedback.gifspacer.gifWhere can I get OLE View?

http://www.microsoft.com/downloads/details.aspx?FamilyID=5233b70d-d9b2-4cb5-aeb6-45664be858b6&displaylang=en.


feedback.gifspacer.gifI want to check for tmp files and, if there, delete them.

Try this:

get-childitem $a | foreach ($_) {remove-item $_.fullpath}


feedback.gifspacer.gif$Count = (get-process | measure-object).Count if you want just to count processes.

Thanks.


feedback.gifspacer.gifDo you plan to cover the interaction between PowerShell and Microsoft Management Console 3.0?

This week, no. In the future, maybe.


feedback.gifspacer.gifYesterday during the live Q-and-A, there was a question about escaping (answer: get-help about_escape_character) and single/double quotation marks. Any hints about the quotation marks?

What kind of hints are you looking for? Do you have questions that the help doesn't answer?


feedback.gifspacer.gifDo you know if there are any issues with trying to run this in a guest OS under Virtual Server or VM?

Nope, haven't tried this yet.


feedback.gifspacer.gifWhy do all graphical elements (e.g. BrowseForFolder) open behind the PowerShell window? Any way to fix this?

We don't know of an easy way to fix that but that's something we're trying to understand ourselves.


feedback.gifspacer.gifDoes MMC 3.0 mean that we can download the final or the beta? Does it support use of PowerShell?

I don't believe that the current version of MMC supports PowerShell.


feedback.gifspacer.gifWith the power of PowerShell, could it replace BAT/VBScript in all aspects?

If you like it a lot it could, but you would need told deploy it on all your machines. But VBS and BAT will be around for a long time.


feedback.gifspacer.gifJust for clarification, is he suggesting that we must use the [System.Reflection...] hack every time we want to use .NET objects?

I'm sorry, but I didn't hear exactly what he said. But no, you shouldn't have to do that each time you want to use a .NET object.


feedback.gifspacer.gifCan PowerShell corrupt or change system configuration on a working system, or is it just an add-on to the OS?

There's nothing in PowerShell that would inadvertently change system configuration. It's an add-on to the OS based on the .NET Framework, and it's very powerful for changing configuration, just like Cmd.exe tools and other scripting languages.


feedback.gifspacer.gifCan we write classes in VB .Net and use them in PowerShell?

Not sure, to tell you the truth. That's a bit out of my league, but you might check the PowerShell programmer's Guide on MSDN: http://search.msdn.microsoft.com/search/Redirect.aspx?title=Windows+PowerShell+Programmer's+Guide&url=http://msdn.microsoft.com/library/en-us/PowerShell_Prog/html/f3aaf667-af84-4ea8-a5ad-d454d0d700b8.asp.


feedback.gifspacer.gifI just saw that Vista has RTM'd. Since PowerShell is integral in Vista does that mean PowerShell will RTM as well?

Actually, PowerShell does NOT ship with Vista. It will remain a separate download for Vista (a download that isn't available yet). We know PowerShell will be released before the end of the year, and we expect that it will be very soon.


feedback.gifspacer.gifCan the team discuss the System.Management.Automation properties that are added to objects? I've run into a conflict with the ParameterizedProperty 'Item' for objects that have an 'Item' Property of their own.

Haven't dealt with those yet, but we'll add it to the list of questions for Q&A after.


feedback.gifspacer.gifWhat would an endless loop cause on PowerShell?

Same as an endless loop in Cmd.exe. You can end it with Ctrl+C


feedback.gifspacer.gifSurprise! MSDN sample code for these classes doesn't have PowerShell examples.

Sadly, no surprise. You're kind of on your own to look at the examples and determine (guess) how to translate everything to PowerShell. I'm sure that will be addressed eventually, but for now ....


feedback.gifspacer.gifHow will PowerShell work in Vista with LUA? Will the recommended way of running be to run as Admin?

$64,000 question for PowerShell and other kinds of scripting. Don't have a definitive answer yet, but when we have one we'll post it on the Script Center.


feedback.gifspacer.gifHas anyone tested to see if PowerShell installed on XP will still be there if you then upgrade to Vista?

At the moment I believe PowerShell prevents you from upgrading to Vista. I seem to recall that you can't do the upgrade until you uninstall PowerShell.


feedback.gifspacer.gifA) Am I able to access WMI on remote machines, and, B) Would I need to install PowerShell on the remote machine also?

Yes, you can use Get-WMIObject to access remote machines. And no, you do not have to install PowerShell on those remote computers.


feedback.gifspacer.gifWhat can cause a Get-WMIObject to fail?

Any number of things. There may not be a class or instance corresponding to what you specify, or you may not have access to the object in question.


feedback.gifspacer.gifHow would you do something like getting all Exchange mailboxes (WMI:\\COMPUTERNAME\ROOT\MicrosoftExchangeV2:Exchange_Mailbox) that are active (not connected to disabled accounts in Active Directory or such) and list stuff like their sizes, number of items together with data from the AD?

To be honest, I don't know; we haven't worked much with Exchange 2007. However, we do have a ton of sample scripts contributed by the Exchange team that can be found here: http://www.microsoft.com/technet/scriptcenter/scripts/message/exch2007/default.mspx. Hopefully one of those will help.


feedback.gifspacer.gifYou might want to let the Vista guys know that the Upgrade Advisor doesn't detect PowerShell.

Thanks, we'll pass that on.


feedback.gifspacer.gifCan PowerShell query Active Directory objects from an admin workstation?

Yes, I believe Dean is going to cover that.


feedback.gifspacer.gifI missed it, sorry: what does CLI stands for?

Command-line interface.


feedback.gifspacer.gifCan I still access Exchange 2000/2003 via CDOEX and CDOEXM from PowerShell?

Same as other COM objects Dean discussed earlier.


feedback.gifspacer.gifHow do you deal with whitespace when trying to run something like powershell.exe -noexit c:\documents and settings\rey\desktop\psscript.ps1? It stops at the 1st whitespace after the word documents.

Try this:

Powershell.exe –noexit &"c:\documents and settings\rey\desktop\psscript.ps1"


feedback.gifspacer.gifDo these object manipulations only last through the current session like variables do?

Yes, they're like objects in other scripting languages in this respect.


feedback.gifspacer.gifWith regards to the whitespace question, I've found that single quotes work wonders.

Thanks.


feedback.gifspacer.gifI mean Exchange 2003. The WMI class is for Exchange 2003.

Oh. In that case you could use Get-WMIObject to connect to the WMI class. I don't know enough about Exchange to give you any details beyond that. If you can't figure it out send an email to scripter@microsoft.com and we'll see if we can help.


feedback.gifspacer.gifThanks, the single quotes worked.

Excellent.


feedback.gifspacer.gifCan PowerShell use Windows Remote Management (WinRM)? Is there an example?

WinRM is a remoting technology that uses Web services, and PowerShell does not yet support native remoting. The only exception I can think of is that you might be able to get at WinRM's .NET Framework classes through new-object and other .NET Framework access methods in PowerShell, but I'm not sure about this.


feedback.gifspacer.gifIs there an option to prevent newline for WRITE-OUTPUT, example: write-output -nonewline ?

I don't believe such an option exists, no.


feedback.gifspacer.gifDean's trying to give away Microsoft Research trade secrets. :) What else can he tell us?

What's it worth to you? Just kidding; Dean is not dealing.


feedback.gifspacer.gifCan the presenter demo the use of System.Windows.Forms?

Probably not today, no. Sorry. Maybe we'll do that in a future webcast.


feedback.gifspacer.gifIn VBScript comments are defined using ' . How are comments defined in PS?

#


feedback.gifspacer.gifWhy not just gm -in $proc?

We'd have to ask Dean, but I guess that notation is too terse.


feedback.gifspacer.gifWhat is a similar command like (grep-linux)/(find windows) to use with get-member?

Don't know if this is what you're after but this command returns the names of all the process properties that start with the letter S:

$a = get-process | gm; $a | where-object {$_.Name -like "s*"}.


feedback.gifspacer.gifWill the link to this event be sent to everyone? I was not able to get into the session until about 30 minutes ago.

I'm not sure. If not, we'll have a link here as soon as it's available: http://www.microsoft.com/technet/scriptcenter/webcasts/ps.mspx.


feedback.gifspacer.gifHow do I get a copy of the PPT for the event on Monday (unfortunately I could not attend)?

You can find a link to the download version here: http://www.microsoft.com/technet/scriptcenter/webcasts/ps.mspx.


feedback.gifspacer.gifI bet Dr. Scripto is smooth with the ladies. Yes?

He's got looks, charm, money -- you bet.


feedback.gifspacer.gifTashkent.

Gesundheit! (I mean, thanks!)


feedback.gifspacer.gifTashkent is the capital of Uzbekistan.

You guys are way too smart for us.


feedback.gifspacer.gifWhen you are talking about .NET objects is that anything that is the assembly cache?

I'm not sure of the terminology, but any .NET Framework class is, in theory, fair game.


feedback.gifspacer.gifPeter is often called "the oldest scripting living guy." I wonder: how old is he?

29. It's just that the rest of us are REALLY young.


feedback.gifspacer.gifIf we request the Monday PDF will you send it to us?

If you can't get it anywhere else, sure. Email scripter@microsoft.com.


feedback.gifspacer.gifSorry if I missed it but what does the "@" symbol do? Such as @{name = "start day";?

I'm not sure. Do you remember where he used this? Was this with adding a new property (member)?


feedback.gifspacer.gifCan we request for the email for Monday's event with the link to the PPT instead? That way our colleagues can enjoy the slide show in all their glory.

If you registered for Monday's webcast, you should have received a follow up email containing a link to the slides. If not, you can request them via scripter@microsoft.com


feedback.gifspacer.gifHere is the code with the "@" Dean used: $proc = get-process | select-object processname, @{name = "start day";expression = {$_.startime.dayofweek}}.

I think what he's doing here is adding a property named Start Day that is equal to the DayOfWeek of the process StartTime. The @ symbol just indicates that a new property (member) is being added.


feedback.gifspacer.gifCan someone ask Dean to clarify the need to specify [system.reflection.assembly]::loadwithpartialname("system.windows.forms") prior to calling the objects?

Will do.


feedback.gifspacer.gifCan you send email with PowerShell?

Not directly. You'd have to use Outlook or CDO or some other program to actually send the mail.


feedback.gifspacer.gif$sc = new-object Net.Mail.SmtpClient -arg "server".

Thanks. That's a perfect example.


feedback.gifspacer.gifI don't think enough people have asked this yet, so I figured I'd give it a shot: When will PowerShell be released? Is it ready yet? Stop teasing us!

Very soon.


feedback.gifspacer.gif$sc.Send("Great@powershell.com", "claudiog64@gmail.com", "Subject", "Here is the mail!").

Thanks, again.


feedback.gifspacer.gifWhere can I get PSmagazine.pdf?

People have been having trouble with the download, but there's a link on the Script Center home page: http://www.microsoft.com/technet/scriptcenter/default.mspx.


feedback.gifspacer.gifHow can we add the loading of "System.Windows.Forms" when PowerShell starts up?

Put the command in your PowerShell profile.


feedback.gifspacer.gifWhy isn’t PowerShell supported in the Longhorn server core?

PowerShell relies on .NET, and .NET is not currently part of the Longhorn core.


feedback.gifspacer.gifI just tried downloading PSMagazine.pdf from the above link. I'm running Adobe Reader 7.0, and it crashes (on multiple computers) trying to download/open this magazine!

Did you save the file to disk and then try opening it? Apparently people were doing that earlier and having success.


feedback.gifspacer.gifAre there any good references/webcasts for C# programming, after Dean’s answer?

There are tons, but I'm a scripter and wouldn't know the best ones. Try searching MSDN, or just a web search. It's system-level programming and it's a vast knowledge domain unto itself.


Top of pageTop of page