Get Local Admins One-Liner

Working with local users and groups can get a little messy in PowerShell. But I wanted to share a quick one-liner you could use to list all the members of the Administrators group. Continue reading

Create New Computer with ADSI

In Windows PowerShell 2.0: TFM, I wrote a short chapter on managing directory services. This chapter was written before Microsoft released their Active Directory module so I discussed how to use the ADSI type adapter. Well it turns out we can’t slip anything past our astute readers. Continue reading

Module Mania

More and more, you’re seeing members of the Windows PowerShell community package their contributions into modules, myself included. Although you’ll probably still see a lot of individual functions because it is often easier to demonstrate or educate. I received a comment on my Weather module that I thought merited a complete post, since I expect there are many administrators still new enough to PowerShell and especially PowerShell 2.0. The question is basically, “I’ve downloaded a module. Now what?”

One major benefit of a module over snapins, is that modules can be deployed with a simple file copy. There is no installation, registration, or the like. Extract the files and PowerShell can use them. But where? Windows PowerShell 2.0 will examine a system environmental variable called %PSMODULEPATH%
[CC LANG=”DOS”]
C:\Users\Jeff\>echo %psmodulepath%
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
[/CC]
The path you see here is for modules that would apply to any user of this computer. Open a PowerShell prompt and look at the variable and you will also see a path that is set for the current user.
[cc lang=”powershell”]
PS C:\> $env:psmodulepath
C:\Users\Jeff\Documents\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
[/cc]
The Modules directory does not exist by default, nor does WindowsPowerShell for that matter, so you might need to create them. Underneath the Modules directory you will have a folder for each module. Thus, you would extract my weather module to a folder called JH-Weather. In that folder are all the module files.

For the most part, these should be the only paths you need. But if you would like to specify an additional module path, you can modify the environmental variable by appending a new path.
[cc lang=”powershell”]
PS C:\> $env:psmodulepath=$env:psmodulepath + “;c:\scripts”
PS C:\> $env:psmodulepath.split(“;”)
C:\Users\Jeff\Documents\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
c:\scripts
[/cc]
Now, PowerShell will also search C:\Scripts for any modules.  HOWEVER..this modification is only for the current PowerShell session. If I open a new PowerShell session, I’ll be back to the same path. If you always need this path, then add the modification command to your PowerShell profile script. Make sure you make the change in your profile before you attempt to import any modules from the new folder.

The Get-Module cmdlet will show all currently loaded modules. If you want to see all the available modules use the -ListAvailable parameter.
[cc lang=powershell]
PS C:\> Get-Module -ListAvailable | Sort Name | Select Name

Name
—-
ActiveDirectory
AdminConsole
AppLocker
BitsTransfer
BSonPosh
DnsShell
DotNet
FileSystem
Godot7
GPAnswers
GroupPolicy
IsePack
JDHTools
JH-Weather
JH-WindowsUpdate
LocalUsersGroups
PowerShellPack
PSCodeGen
Pscx
PSDiagnostics
PSImageTools
PSRSS
PSSystemTools
PSUserTools
scripts
ShowTree
TaskScheduler
TroubleshootingPack
WebAdministration
WPK
[/cc]

When I want to use a module I import it into my session.
[cc lang=”powershell”]
PS C:\> Import-Module WebAdministration
[/cc]
Use Get-Command to see the module contents.

Creating modules is a completely different topic for another day, but I hope this has helped get you started in the right direction regarding what to do with modules. If not, let me know. There is also a help topic, About_Modules you should read. Don Jones and I also cover modules in Windows PowerShell 2.0: TFM

Get Your Free Scripting Toolkit

If you were at this year's TechEd event in New Orleans, I hoped you dropped by the SAPIEN Technologies booth and picked up your free Scripting Toolkit. What's that you say? Check it out on the SAPIEN blog and then download your free copy.

Out-Clip

I’ve started working on the 2nd edition of Managing Active Directory with Windows PowerShell: TFM. As with almost all of my writing projects it will be full of PowerShell code examples. In the past I’ve always relied on a manual copy and paste to add content to the manuscript. The PowerShell Community Extensions made this a little easier in the past with their Out-Clipboard cmdlet. That made it easier to grab the results so I could paste them in my Word document. But I also needed the PowerShell command which meant another copy and paste. What I really needed was a way to grab the expression AND the result and send both to the Windows clipboard. Thus was born Out-Clip.














































































































































































































































The function requires the Write-Clipboard cmdlet from the PowerShell Community Extensions. I use a Try/Catch block to verify the cmdlet exists in your current session. If it doesn’t, you get an error message and the pipeline is terminated. You’ll need to make sure you manually load the module before running this function.

The function itself is pretty basic I think. You can specify one or more scriptblocks either with the –Scriptblock parameter or piped into the function.

PS C:\> out-clip {get-service spooler | select *}

Since I prefer almost all of my code examples to use a prompt of PS C:\>, the function prepends it before the scriptblock command. If you prefer to see the actual or current location, use the –UseCurrentLocation parameter. If you prefer no prompt, then use –NoLocation. Because these two values are mutually exclusive, I’ve used two parameter sets, which you can see when you look at help.

PS C:\> get-help out-clip

NAME

Out-Clip

SYNOPSIS

Capture a PowerShell command and its result to the Windows clipboard.

SYNTAX

Out-Clip [-ScriptBlock] <ScriptBlock[]> [-UseCurrentLocation] [<CommonParameters>]

Out-Clip [-ScriptBlock] <ScriptBlock[]> [-NoLocation] [<CommonParameters>]

DESCRIPTION

Using Write-Clipboard from the PowerShell Community Extensions,

this function will take a Windows PowerShell scriptblock, execute

it and write both the command and the results to the Windows clipboard.

You can also pipe a collection of scriptblocks to this function or use

a comma separated list with the -Scriptblock parameter.

By default the output will include the PS prompt set to PS C:\>.

If you want to use the actual current location then specify

-UseCurrentLocation. If you prefer no prompt at all then use -NoLocation.

RELATED LINKS

Write-Clipboard

Set-Clipboard

Out-Clipboard

Get-Clipboard

REMARKS

To see the examples, type: “get-help Out-Clip -examples”.

For more information, type: “get-help Out-Clip -detailed”.

For technical information, type: “get-help Out-Clip -full”.

Out-Clip uses a hash-table to store the scriptblock command and the result. If the scriptblock fails, then the last error is grabbed and used instead for the result. Thus I always get the command and result in the clipboard, even if the command failed. After all scriptblocks have been evaluated the hash-table’s contents are piped to Write-Clipboard.














The script file also defines an alias, oc, for the function since I don’t like to type anymore than I have to.

Granted, this function meets a pretty specific need, but it does demonstrate some PowerShell 2.0 scripting features such as multiple parameter sets, Try/Catch and hash tables. If you try it out, I hope you’ll let me know what you think.