About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Monday, January 28, 2008

Some Unix commands for PowerShell

I've been playing with PowerShell a little and decided to make some Unix commands that were not ported over.

 

Command Function
whoami

function whoami
{  [System.Security.Principal.WindowsIdentity]::GetCurrent().Name       
}

~ (works better if it was not an alias)

function ~
{
   $home
}

mkdir

function mkdir([string] $name)
{
   New-Item $name -type directory
}

touch

function touch([string] $path = ".\", [string] $name)
{
   $file = $path + $name
   $result = test-path $file
   if($result -eq $true)
   {
      ##"Changing LastWriteTime"
      gci $file | foreach{$_.lastwritetime = $(get-date)}
   }
   else
   {
      ##"Creating new file"
      new-item $file -type file
   }
}

find (still currently working on this one. I need to implement wild cards)

function find([string] $name = "", [string] $ext = "", [string] $drive = "C:")
{
   $computer = "."
   if($name -ne "")
   {
      $files = get-wmiobject -computer $computer cim_datafile -filter "FileName = '$name'"
   }
   if($ext -ne "")
   {
      $files = get-wmiobject -computer $computer cim_datafile -filter "Extension = '$ext'"
   }

   $files | add-member --type PropertySet "FindInfo" ([string[]]("Name","FileType","Extension","CreationDate")) -force
   $files | select FindInfo | ft -wrap
}

 

I figured re-writing Unix commands might help me learn powershell. I'm going to use http://en.wikibooks.org/wiki/Guide_to_UNIX/Commands as source of basic Unix commands to implement.

No comments: