powershell

How to programatically determine what PowerShell snapin are installed

Hi, I would like to know if their is a method which I can call which can tell me if a PowerShell snapin is installed. I know I could call probably do it, say via WMI or write a PowerShell script and return a list to C#, but is their not a method do it somewhere. Thanks. ...

virtualenv in PowerShell?

Hi fellow pythonistas, there seems to be a problem when virtualenv is used in PowerShell. When I try to activate my environment in PowerShell like.. > env/scripts/activate .. nothing happens. (the shell prompt should have changed as well as the PATH env. variable .) I guess the problem is that PowerShell spawns a new cmd. process jus...

Can a Batch script know if it's called from PowerShell?

Is there any way a batch script can know if it's called from PowerShell (without extra parameters feeded)? Need something like.. if (%THIS_BATCH_CALLED_FROM_POWERSHELL%) ... warn the user or drop back to powershell to execute the proper instructions... Question related with this question - virtualenv-in-powershell. ...

Can you set an object's DefaultDisplayPropertySet in a PowerShell v2 script?

Here is a blog post from Kirk Munro that explains how a script can set the DefaultDisplayPropertySet on its output objects: Essential PowerShell: Define default properties for custom objects His technique and example code doesn't appear to work in PowerShell v2. (Note, I have PowerTab and PSCX installed--perhaps those could be interfer...

PowerShell Scripting - Get-ChildItem

I'm just learning PowerShell and I've written a script that will be used for archiving log files from a server. I'm in pretty good shape with everything but the recursiveness or not of get-childitem... The issue I seem to be having is that when Get-ChildItem is not recursive and -Include is present with only one filter, it is ignored! ...

PowerShell Script to Disable/Re-enable a TCP/IP Port

Is there a way to disable and re-enable a known TCP/IP port in PowerShell? ...

PowerShell: Copy-Item Cannot find path

I'm trying to get PowerShell to copy files from a remote computer (on which I have admin rights through AD) to the local computer. It fails in the strangest place. Here's a snippet of the script: $configs = Get-ChildItem -Recurse -ErrorAction SilentlyContinue -Filter "*.config" $serverUNCPath foreach($config in $configs){ $config_...

Writing a PowerShell cmdlet to accept a scriptblock as a parameter

The Rename-Item accepts a scriptblock (I think...) as a parameter so I can do something like this if I want to rename a bunch of files (for example): Dir *.ps1 | Rename-item -NewName {$_.Name -replace ".ps1" ".txt" } I'm writing a cmdlet to rename some items and would like to be able to use syntax like this for the new name, but how t...

How to read cell data in excel and output to command prompt

Hi All, I'm a sys admin and I am trying to learn how to use powershell... I have never done any type of scripting or coding before and I have been teaching myself online by learning from the technet script centre and online forums. What I am trying to accomplish is to open an excel spreadsheet get information from it (usernames and pas...

Powershell trap not being triggered consistently.

I can't understand why I'm seeing this behavior in Powershell: PS C:\> trap { "Got it!" } 1/0 Attempted to divide by zero. At line:1 char:22 + trap { "Got it!" } 1/0 <<<< PS C:\> trap { "Got it!" } 1/$null Got it! Attempted to divide by zero. At line:1 char:22 + trap { "Got it!" } 1/$ <<<< null Why does one expression trigger the tra...

Is PowerShell worth learning?

I played around with PowerShell yesterday, and it seemed pretty nice ( the GUI at least ). Is it worth getting to know the language? I'm pretty proficient in Ruby/Python/Perl/Groovy when it comes to scripting languages, so I think the fact that PowerShell is a scripting language isn't a bonus here. Another thing I didn't like was that P...

PowerShell Add-Member to array items

I'm trying to use the PowerShell Add-Member cmd on all the items in an array and then access the member I added later but it doesn't show up. You can see in the output of the below code that the NoteProperty appears to exist within the scope of the foreach statement but it doesn't exist on the same object outside of that scope. Any way...

Jagged PowerShell array is losing a dimension when only one element exists

I have the following PowerShell function that works well for any input except for 1. If I pass it an input of 1 it will return an array with two elements 1,1 instead of a single element which is itself an array of two elements (1,1). Any ideas how I can make PowerShell return a jagged array with one element that is itself an array? fu...

Removing duplicate values from a PowerShell array

How can I remove duplicates from a PowerShell array. $a = @(1,2,3,4,5,5,6,7,8,9,0,0) ...

Is it possible to call powershell cmdlets asynchronously?

Say i have cmdlet1 and cmdlet2, both are long running tasks. Normally i would have a batch file which calls the two cmdlets in order: call powershell cmdlet1 call powershell cmdlet2 Is there anyway to set them off asynchronously? ...

PowerShell Job Progress Monitoring

Is there a way for a PowerShell job to report progress or to trigger events before it is complete? I just started playing around with executing background jobs in PowerShell and am wondering how far I can push the capability. ...

Ignoring an errorlevel != 0 in Windows Powershell

I have a script that runs an external exe. When that exe fail (sets errorlevel to 1), the powershell script fails. I'm running curl.exe and getting this: + CategoryInfo : NotSpecified: ( % Total % ... Time Current:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError How can I ignore/catch the failure of the externa...

Active Directory Filter memberof

I am trying to get all of the CN's out of active directory in order to populate groups based on that name into Sharepoint Services. I can list the "memberof" section but I can not seem to split it using split(",") $Dom = 'LDAP://OU=External,OU=Users,OU=HomeOffice,DC=mydoman,DC=com' $Root = New-Object DirectoryServices.DirectoryEntry $D...

Calling a specific powershell function from command line

I have a powershell script that contains several function. How do I invoke, from the command line, a specific function? This doesn't work: powershell -File script.ps1 -Command My-Func ...

Output data with no column headings powershell

I want to be able to output data from Powershell without any column headings. I know I can hide the column heading using Format-Table -HideTableHeaders, but that leaves a blank line at the top. Here is my example: get-qadgroupmember 'Domain Admins' | Select Name | ft -hide | out-file Admins.txt How do I eliminate the column heading ...