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).
Showing posts with label command. Show all posts
Showing posts with label command. Show all posts

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.

Wednesday, January 16, 2008

How to make a new command in Windows PowerShell

Lets say you have a idea for a new function that you would like to implement in Window's PowerShell. In this case I made a Unix like whoami command:

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

when in PowerShell it returned for me: mss/wandrus. Simple enough. Now to get the command to be usable every time an new PowerShell is opened.

 

Type within PowerShell:  Get-Variable profile | Format-List

This should give you the information on what file and directory path are needed to place your custom scripts. In this case I was given:

C:\Document and Settings\wandrus\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

 

Which meant that I had to create a new folder (WindowsPowerShell) and a new script file in notepad (Microsoft.PowerShell_profile.ps1) where I've place my function.

Monday, October 15, 2007

Command Line short-cut variables

%windir% = value from GetWindowsDirectoryW
%systemroot% = value from GetWindowsDirectoryW
%systemdrive% = first two characters from GetWindowsDirectoryW
%systemdir% = value from GetSystemDirectoryW
%username% = value from GetUserNameW
%programfiles% = value from ShGetFolderPathW(..., CSIDL_PROGRAM_FILES, ...)
%userstartmenu% = value from ShGetFolderPathW(..., CSIDL_STARTMENU, ...)
%allstartmenu% = value from ShGetFolderPathW(..., CSIDL_COMMON_STARTMENU, ...)
%userdesktop% = value from ShGetFolderPathW(..., CSIDL_DESKTOPDIRECTORY, ...)
%alldesktop% = value from ShGetFolderPathW(..., CSIDL_COMMON_DESKTOPDIRECTORY, ...)
%userfavorites% = value from ShGetFolderPathW(..., CSIDL_FAVORITES, ...)
%allfavorites% = value from ShGetFolderPathW(..., CSIDL_COMMON_FAVORITES, ...)
%userappdata% = value from ShGetFolderPathW(..., CSIDL_APPDATA, ...)
%allappdata% = value from ShGetFolderPathW(..., CSIDL_COMMON_APPDATA, ...)
%allusersprofile% = value from GetAllUsersProfileDirectoryW
%userprofile% = value from GetUserProfileDirectoryW
%appexedir% = value from GetModuleFileNameW