powershell

Ignore 'Security Warning' running script from command line

I am trying to execut a script from shared folder that I trust: powershell -file "\\server\scripts\my.ps1" But get security warning, and have to press 'R' Security Warning Run only scripts that you trust. While scripts from the Internet can be useful, this script can potentially harm your computer. Do you want to run \s...

Can't redirect PowerShell output when -EncodedCommand used.

For my tests I am using 'Start > Run' dialog (not the cmd.exe). This works fine, and I get 9 in log.txt powershell -Command 4+5 > c:\log.txt But this does not work: powershell -EncodedCommand IAA1ACsANwAgAA== > c:\log.txt So how can I redirect output in this case? Experimental code: function Test { $cmd = { 5+7 } $encode...

Exclude list in Powershell Copy-Item does not appear to be working

I have the following snippet of Powershell script: $source = 'd:\t1\*' $dest = 'd:\t2' $exclude = @('*.pdb','*.config') Copy-Item $source $dest -Recurse -Force -Exclude $exclude Which works to copy all files and folders from t1 to t2 but it only excludes the exclude list in the "root"/"first-level" folder and not in sub-folders. Anyb...

Why is there trailing whitespaces on hashtable keys?

Hello, I don't understand why there's a difference on string concatenations in this script. I really don't understand where the whitespace is coming from? Can you tell me what's wrong? $table = @{ "aaa"=1 } $x = "qqq" $y = "rrr" $table.GetEnumerator() | ForEach-Object { Write-Host $_.Key$x #THIS PRINTS "aaa qqq" } Wr...

Powershell v1: Is it possible to assign the result of a switch statement to a variable?

Is it possible to assign the result of a switch statement to a variable. For example, instead of: switch ($Extension) { doc {$Location = "C:\Users\username\Documents\"; break} exe {$Location = "C:\Users\username\Downloads\"; break} default {$Location = "C:\Users\username\Desktop\"} } Is it possible ...

Add a "Hyperlink" item type to a list using PowerShell in Sharepoint

I've been a SharePoint admin for a while, and now have been tasked with a bit more of a developer role - which I'm still very much learning. Most things I've been able to figure out on my own or through Google, but this one has me stumped. For one particular task I need to use PowerShell to script adding items to a list. Normally - not ...

What can I do with C# and Powershell?

I have a very good understanding of C# (I'm probably an intermeddiate programmer) and a very basic understanding of powershell. I'm using Windows PowerShell CTP 3, which has been really fun. But I want to go beyond writing scripts/functions. Is there any cool stuff to do with C#? ...

Powershell impersonation

How can I do an impersonation in PowerShell? I can use advapi32.dll LogonUser, but maybe there is a simpler solution? ...

How can I dispose System.Xml.XmlWriter in PowerShell

I am trying to dispose XmlWriter object: try { [System.Xml.XmlWriter] $writer = [System.Xml.XmlWriter]::Create('c:\some.xml') } finally { $writer.Dispose() } Error: Method invocation failed because [System.Xml.XmlWellFormedWriter] doesn't contain a method named 'Dispose'. On the other side: $writer -is [IDisposab...

How can I call explicitly implemented interface method from PowerShell?

Code: add-type @" public interface IFoo { void Foo(); } public class Bar : IFoo { void IFoo.Foo() { } } "@ -Language Csharp $bar = New-Object Bar ($bar -as [IFoo]).Foo() # ERROR. Error: Method invocation failed because [Bar] doesn't contain a method named 'Foo'. ...

Converting xml from UTF-16 to UTF-8 using PowerShell

What's the easiest way to convert XML from UTF16 to a UTF8 encoded file? ...

C# Powershell Interop

The Sys Admin guy is writing some common housekeeping Power Shell scripts. Predominantly for AD management (updating exchange details, moving people around security groups etc.) I'd like to use these scripts from C# (I intend to write it as a library, consumed by a web site). I've seen this code project article which looks interesting ...

How can I add multiple ServerBindings to IIS6 using powershell?

I'm having real issues trying to add multiple serverbindings to a single website using powershell! Currently i have the following code: function UpdateMetaBaseProperties($dirEntry, $Properties) { foreach($Prop in $Properties) { $KeyValue = $Prop.Split('='); $dirEntry.psbase.Invoke("Put", ($Ke...

Running PowerShell file in C#, how to do that in an ASP.NET form?

I have made a PowerShell script, which is running perfectly fine and generating a text file when I run it standalone. I wanted to automate that whenever my ASP.NET page loads I invoke a process from C# that calls my PowerShell script and executes that leading to a text file being generated. The problem is the script is being called, but...

Testing Powershell scripts for correctness

Hi, Is their anyway to test the correctness of a powershell script without executing it. I know you could do something similar in unix, but can't find the equiv for powershell. Essentially what I have is a script repository, where each script is tied to rule. If a rule fires, the script executes, but I need to be sure the script is v...

PowerShell Get -Date

Hi guys I have this in PowerShell: I have a collection that have a CustomProp named "rep_date" that contains a Date in format: mm/dd/yyyy, now I want to save there the date in this format: dd/mm/yyyy, Im trying this approach: For ($i=0;$i –le $HD.count; ++$i) { $B = $HD[$i].CustomProps[‘rep_date’] = Get-Date –date $HD[$i].CustomPro...

Get powershell to display all paths where a certain file can be found on a drive.

I'm trying to build a function that will show me all path's where a certain filename is located. The function would take one parameter, that being the file name. The result would be either a list of all paths, or a message saying there's no such file on the system. I'm new to Powershell, and I'm not getting the syntax just yet. I've tri...

Powershell ps1 file won't run from within Powershell

I made a Powershell function just now and saved it to a ps1 file. However, when I try to execute it from within powershell, it won't run. I've allready changed to the settings for running unsigned code by entering this command: set-executionpolicy remotesigned The function is this: Function listAllPaths([string]$fromFolder, [string]...

Powershell problem with internal logic.

I've got a weird thing going here in Powershell 1.0 and I don't understand why Powershell is reacting the way it is. Situation: I've created several zip files on a drive by the letter e: Now I want to delete them all. But there are more zip files on my e: drive. So I want to delete only the ones in this specific folder and its subfolde...

Powershell load all functions into powershell from a certain directory

Suppose you're a system admin who uses powershell to managa a lot of things on his/her system(s). You've probably written a lot of functions which do things you regularly need to check. However, if you have to move around a lot, use different machines a lot and so on, you'd have to re-enter all your functions again and again to be able ...