powershell

How can I find the source path of an executing script?

I want to be able to tell what path my executing script was run from. This will often not be $pwd. I need to call other scripts that are in a folder structure relative to my script and while I could hard code the paths, that's both distasteful and a bit of a pain in the neck when trying to promote from "dev" to "test" to "production". ...

Powershell pitfalls

What Powershell pitfalls you have fall into? :-) Mine are: # ----------------------------------- function foo() { @("text") } # Expected 1, actually 4. (foo).length # ----------------------------------- if(@($null, $null)) { Write-Host "Expected to be here, and I am here." } if(@($null)) { Write-Host "Expected to be her...

How to loop through a dataset in powershell?

I am trying a "very" simple task to output values of each rows from a dataset : $Data = '' for($i=0;$i -le $ds.Tables[1].Rows.Count;$i++) { write-host 'value is : '+$i+' '+$ds.Tables[1].Rows[$i][0] } OUTPUT : value is : +0+ +System.Data.DataSet.Tables[1].Rows[0][0] value is : +1+ +System.Data.DataSet.Tables[1].Rows[1][0] value i...

How do I return only the matching regex when i select-string(grep) in powershell

Hello, I trying to find a pattern in files, but i when i get a match using select-string, i don't want the entire line, i just want the part that matched. Is there a parameter i can specify to do this? Ex: If i did select-string .-.-. and the file contained a line with: abc 1-2-3 abc I'd like to get a result of just "1-2-3" inst...

PowerShell Script to Get a Directory Total Size

I need to get the size of a directory, recursively. I have to do this every month so I want to make a PowerShell script to do it. How can I do it? ...

Powershell. C# Measuring powershell host memory usage.

Hi, I'm automating the usage of powershell scripts from C# using the powershell async pipeline invoke and I notice that when I execute script over and over again, using the same runspace, the .vhost.exe memory usage goes up and up and up, and it would run out of memory after a days operations. Is there a way to measure how much me...

Powershell Command Processing (Passing in Variables)

I'm creating a Powershell script to deploy some code and part of the process is to call a command-line compression tool called RAR.EXE to back-up some folders. I'm attempting to dynamically build out the parameters and then have powershell call the command with the variables but I'm running into trouble. It isn't working... Run the fo...

How to use @ character in powershell?

Completely newbie simple powershell question I am trying to do the following - for each *.sql file in the current directory run sqlplus username/password@connect_identifier_specified_in_argument @file_name Here is what I have so far: $scripts = dir *.sql foreach($script in $scripts) { write-host sqlplus username/password"@"$args ...

PowerShell Script in PostBuild

Continuous Integration I have been working on a PowerShell script to keep our development process streamlined. I was planning on running it as a post-build event, but I'm having some trouble. From the PowerShell prompt, the following works wonderfully: PS C:\> ./example.ps1 However, when attempting to run this from cmd.exe as follo...

How to display twitter statuses (pseudo html) using wpf (powerboots)

I have a text like this: 'Rails on IIS7 http://is.gd/vWPn' (it's a tweet by shanselman). I'd like to display it somehow using wpf. How should I preprocess the text and what controls use then? Current solution is that I create 2 controls: Label and Hyperlink and add them into a TextBlock. In powerboots it looks like this: boots { borde...

How can I get the current PowerShell executing file?

Note: PowerShell 1.0 I'd like to get the current executing PowerShell file name. That is, if I start my session like this: powershell.exe .\myfile.ps1 I'd like to get the string ".\myfile.ps1" (or something like that). EDIT: "myfile.ps1" is preferable. Any ideas? ...

Find out whether a file is a symlink in Powershell

Edit: Changed the question title since the answer explicitly only handles Symlinks. I am having a Powershell script which is walking a directory tree and sometimes I have auxiliary files hardlinked there which should not be processed. Is there an easy way of finding out whether a file (i. e. System.IO.FileInfo) is a hard link or not? I...

Weird Powershell performance issue

NOTE: This is a solution for Project Euler problem 14. If you still want to solve it yourself then don't read on. The problem was to find the number below one million which, as starting number for the Collatz sequence, produces the longest such sequence. My initial code was as follows: $r = @{} for($i = 1; $i -lt 1000000; $i++) { ...

How to call method with output parameters in Powershell?

I'm working on a script to get started in Powershell. I'm trying to convert a working VBScript script that enumerates mapped network drives on a remote Windows computer. One of the tasks is to use remote WMI to read the registry and find the process owner of explorer.exe in order to determine who is logged in. This seems easy enough g...

How do I use PowerShell to Validate XML files against an XSD?

As a part of my development I'd like to be able to validate an entire folder's worth of XML files against a single XSD file. A PowerShell function seems like a good candidate for this as I can then just pipe a list of files to it like so: dir *.xml | Validate-Xml -Schema .\MySchema.xsd I've considered porting C# code from the Validati...

Transfer List items with Attachments from SharePoint 2003 to an existing list in SharePoint 2007

I have items in a list in a SharePoint 2003 site that I need to add to an existing list in a 2007 site. The items have attachments. How can this be accomplished using PowerShell or C#? ...

Powershell script to delete old files

The following script will delete files in a named directory that are older than 14 days and write to a .txt with the path and the files deleted (found this script on another forum..credit to shay): dir c:\tmp -recurse | where {!$.PsIsContainer -AND $.lastWriteTime -lt (Get-Date).AddDays(-14) } | select LastWriteTime,@{n="Path";e={conver...

Powershell Calling .NET Assembly that uses App.config

Hi, I have a Powershell script that is loading a .NET assembly (.EXE in my case) and calling a public method that uses the app.config to pull an encrypted connection string. The script dynamically copies the assembly's exe.config over to the Powershell ($pshome) folder as powershell.exe.config and is able to run from my development box...

Why does Powershell's "return" keyword cause type errors?

$xml = [xml] '<node>foo</node>' function foo2 { return "foo2" } # all of these fail with the message: # **Cannot set "foo" because only strings can be used as values to set XmlNode properties.** $xml.node = foo2 $xml.node = foo2 -as [string] # because of this issue[1] $xml.node = (foo2) # these work $xml.node = (foo2).tostring() $xm...

PowerShell, Extension Methods, and Monkey Patching

Is it possible to write extension method in PowerShell? or to bolt a new method on top of an existing type like [string] live at runtime? ...