Mats codemix

Mats codemix

A little c#, a litte .NET and throw in some c++ and you get a nice Spaghetti

Mats codemix RSS Feed
 
 
 
 

Archive for Powershell

Powershell tips - Filter files modified the last n days

I wanted to list files modified the last n days. Here is a filter function i wrote to help me.
1 function FilterLast([int] $nDays)
2 {
3 begin {
4 $DateToCompare = (Get-date).AddDays(-1 * $nDays)
5 }
6
[...]

Windows Special folders in powershell…

… or where is my wallpaper?
I wanted to edit the imagefile i had as windows desktop background so i wanted to find out the location of it. Excellent material for a powershell lession i thought.
So where are the special folders? Could it be here:
ls ([Environment]::GetFolderPath(’MyPictures’)
Found help about how to get windows special folders  here and [...]

Powershelll tips - Filter on container

Say you want to recurse a diretory tree and show just the directories.
Yo can write
ls C:\ -rec | %{if ($_.PSIsContainer) { $_ }} | %{$_.Fullname}
Add a little filter function
1 function FilterContainer()
2 {
3 process
4 {
5 %{if ($_.PSIsContainer) { $_ }}
6 [...]

Calling 7-Zip from powershell

I had to write a little utility-script to backup and restore some files used for testdata. First i tried a commandline plugin for IZarc which seemed to select the wrong set of files…
I decided to use 7-Zip after reading this post:John Robbins blog which helped me with the -o option.
What i struggled with was how [...]

Powershell v2 CTP3 - profiles

I wanted to change the colours for powersshell error messages. Easily done by using this code

# Set Error and warning colours
$hostprivate = (Get-Host).PrivateData
$hostprivate.ErrorBackgroundColor = "red"

First i put the code in Profile.ps1 in C:\Documents and Settings\myname\Mina dokument\WindowsPowerShell.
This caused the powerhell scripting environment (ISE) to spit out some errors while starting:
Property ‘ErrorBackgroundColor’ cannot be [...]

Testing geshi

Use Geshi (wp-syntax)
Below should be nice looking colour coded pwoershell code

# Remember to write pre lang="powershell" NOT posh or something other cryptic….
Add-Content powershell
Add-History

End of Geshi example

Powershell-script to find files not having specified text

I had the need to find files “not containing” a certain text. I scanned the web and found all and nothing. a few dedicated search tools and editors (some payware and some free). Since i use powershell a lot in my work i wanted to use that environment.  Finding a file based on a ceratin [...]