powershell

PowerShell: How to mount drive programatically from SnapIn

I am creating a suite of custom cmdlets and providers in C#. I also have a PowerShell SnapIn that takes care of registering the cmdlets and providers. I have exported a console session so that I can start PowerShell with the -PSConsoleFile parameter to automatically load my snap in. I also want to mount a drive whenever I run PS wit...

Powershell causes a: to seek when it starts; how do I stop this?

I've written a powershell script that I've turned into a scheduled task. So far, so good. The issue I have is that every time Powershell is started, it causes my floppy drive to seek; this means every five minutes (on the task schedule), I get a little "grind" from the floppy drive. How do I disable this behavior? C:\fa>powershell ...

How to make an authenticated web request in Powershell?

In C#, I might do something like this: System.Net.WebClient w = new System.Net.WebClient(); w.Credentials = new System.Net.NetworkCredential(username, auth, domain); string webpage = w.DownloadString(url); Is there a Powershell version of this, or should I just call through to the CLR? ...

Credential storage best practices

I'm writing a Windows service and need to make authenticated web requests. The service will not be running under the ownership of the credentials used to make the request; this implies that I need to store the credentials for the request in some way. What are the best practices here? The credentials will need to be stored in App.config ...

powershell script exits in "if" clause if run from command prompt

I am feeling surprised by the difference between two seemingly identical scripts. first.ps1: "A" if ($true) { "B" } "C" second.ps1: "A" if ($true) { "B" } "C" Now open a CMD window, and run these scripts like this: powershell - < first.ps1 powershell - < second.ps1 first produces: A B C while second produces jus...

PowerShell - How do I test for a variable value with "Set-StrictMode -version latest"?

I've just started doing some PowerShell scripting, and I'm running into a problem testing variables for a value. I try to run everything with all warnings enabled, especially while I'm learning, in order to catch dumb mistakes. So, I'm using CTPV3 and setting strict mode on with "set-strictmode -version latest". But I'm running into a ro...

What is Windows PowerShell?

Can someone please explain what PowerShell is? I have Windows 7 installed, and I noticed that it has PowerShell. I remember installing this a while back when signing up for Windows Azure, but only because it was required. Can someone please explain this in uber-layman's terms? :) ...

Powershell parsing to simulate "grep -C 2" in V1

I'm trying to dig through some logs and need information before and after the line I can match on. How would I do this in powershell ala "grep -C 2"? Edit: in version 1 --I can't wait for r2, then I get to put it on production machines :) ...

script to save file as unicode

Do you know any way that I could programmatically or via scrirpt transform a set of text files saved in ansi character encoding, to unicode encoding? I would like to do the same as I do when I open the file with notepad and choose to save it as an unicode file. ...

Bash/DOS/PowerShell script to list most recent versions of files?

We have a list of (let's say 50) reports that get dumped into various folders depending on certain conditions. All the reports have standard names eg. D099C.LIS, D18A0.LIS etc. Sometimes a report can exist in up to 5 different locations, and I need to generate a list of all the locations of the most recent version of each report. I ca...

Why is XDocument.Descendants() returning IEnumerator in PowerShell ISE?

I'm writing a PowerShell script to manipulate some Windows Installer XML (WiX). I'm using the new XML APIs in .NET 3.5 to do this, as I find it an easier API to work with than the DOM. The following script fragment is flatly refusing to work: [System.Reflection.Assembly]::LoadWithPartialName("System.Xml.Linq") | Out-Null # Basic ide...

Using PowerShell with .NET 3.5 runtime/libraries

Is it possible to run PowerShell 1.0 (or 2.0 CTP) backed by the 3.5 runtime instead of 2.0? We're building a .NET 3.5 solution, and I'd still like to use PowerShell as our scripting engine for scheduled tasks, etc. I don't need LINQ syntax or anything, just the 3.5 libraries and runtime. FOLLOWUP: thank you for the reply about dynam...

powershell exchange : how do you screen for legacy or linked mailboxes?

I've noticed when I pull up exchange management console, it shows mailboxes which have the "Recipient Type Details" as Legacy Mailboxes. How do i go about querying which ones are legacy, user or linked mailboxes? I've tried get-mailbox -identity | select deleteditemflags but that doesn't seem to work. thanks in advance ...

Execute PowerShell Script from C# with Commandline Arguments

I need to execute a PowerShell script from within C#. The script needs commandline arguments. This is what I have done so far: RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create(); Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration); runspace.Open(); RunspaceInvoke scriptInvoker = new Run...

How to ignore escape sequences stored in PowerShell string variable?

In my PowerShell script, I'm running Select-String over a number of files, looking for a string passed into it via a variable ($id): foreach ($file in (ls "path\to\files")) { $found = $false $found = Select-String -Path $file $id -Quiet if ($found) { break } } Unfortunately, the $id variable sometimes things li...

PowerShell - Distributed Solution

I'm new to PS so I may get some of the terminology wrong. If you want to roll out a custom PowerShell environment (snap-in) for a team of 30 developers/DBAs. What is the best way to do this... if you intend to be rolling out new functionality once a week? Does PowerShell 2.0 help in this regards? Assumption: There is no issue with eve...

What does it mean that powershell 2.0 will be "firewall friendly"?

I'm interested in executing powershell scripts on a computer behind a firewall. What ports will I need to have open? Will any special configuration be needed or I'll be just be able to connect to a fresh install of Windows Server 2008 r2 and start executing my scripts? ...

Future of cmd & powershell

We were just today discussing it, so I went on a little search but found nothing, zip, nada. What is the future of ms's cmd shell? Do they intend to replace it completely with powershell in the future versions of windows, or just ship powershell as a parallel alternative ? Does anyone have any links, articles, ... whatever regarding th...

powershell exchange : if then statement syntax?

In my script i'm trying to test for true and false. Is this syntax incorrect? $pdaout = "" if ($pda.ActiveSyncEnabled.tostring() -like "True") {$pdaout = "TRUE"} if ($pda.ActiveSyncEnabled.tostring() -like "False") {$pdaout = "-"} write-host $pdaout ...

Start app from PowerShell executing in custom runspace, keep app running, finish script?

I have a custom PowerShell runspace from which I execute a script (simplified): Pipeline pipeline = myRunSpace.CreatePipeline(@"c:\temp\Myscript.ps1"); Collection<PSObject> results = pipeLine.Invoke(); In the script I do: # c:\temp\MyScript.ps1 notepad.exe Now the Invoke() returns when notepad is closed. Is there a way to start an...