Sunday, June 5, 2022

[SOLVED] powershell script to select directory

Issue

I would like to create a powershell function or cmdlet that would allow me to quickly navigate to files and function like the example below. Could someone enlighten me to what the "/D" would be called? Is it a parameter?

C:\Windows\system32> folder /help
Usage: Select directory folder [/? | /Username | /S | /D] 

No args    Display help. This is the same as typing /?.
/?         Display help. This is the same as not typing any options.
/Username  Change to the "Username" directory
/S         Change to the "scripts" directory
/D         Change to the "desktop" directory

C:\Windows\system32> folder /username
C:\Users\username> folder /S
C:\Users\username\desktop\scripts> folder /D
C:\Users\username\desktop>

This is what I have so far. This will only take me to the desktop directory, how would I write this to have it named "folder" and add the modifier/parameters?

function desktop { cd "C:\users\username\desktop" }

This functions as following:

C:\Windows\system32> desktop
C:\Users\username\desktop>

Solution

Yes, in PowerShell lingo /D is called a parameter (other shells may call it an option or a flag, as Abraham Zinala notes), but note that PowerShell only accepts - as the parameter-name sigil, so it would have to be -D (on invocation you're free to use -d or -D, because parameter names aren't case-sensitive in PowerShell).
A parameter that acts as an on/off switch (called a flag in other shells, i.e. one that doesn't take an argument), is called a switch [parameter] in PowerShell.

Note that while you can define single-letter parameter names in PowerShell, it is customary to use longer, descriptive names, e.g. -Desktop:

  • Thanks to what PowerShell calls elastic syntax, you can still use just -d on invocation, as long as the d unambiguously implies the full target parameter name (e.g., -Desktop).

  • Alternatively, you may explicitly declare -d to be an alias for -Desktop, by decorating the parameter declaration with an [Alias()] attribute.

Here's a sample function that works slightly differently from what you specified, but operates in a more PowerShell-idiomatic way:

  • Parameter-less invocation changes to the current user's home folder.

    • To target a different user's home folder, pass the username to the -u / -UserName parameter.
  • Whatever user's home folder is chosen in a given invocation can optionally be combined with either -Desktop / -d or -Scripts / -s, in order to change to the desktop / scripts folder for the targeted user.

  • To show help, use -? (or Get-Help folder), which PowerShell supports by default, showing the command's syntax.

    • To add a verbal description to the help, you can use comment-based help to your function - see the conceptual about_Comment_Based_Help help topic.
  • Push-Location rather than Set-Location (whose built-in alias is cd) is used to change location, to enable returning to the previous folder with Pop-Location (whose built-in aliases are popd and popl)

  • The function below is a so-called advanced function, which means that it acts like a (binary) cmdlet; for more information, see the conceptual about_Functions_Advanced and about_Functions_Advanced_parameters help topics.

function folder {
  [CmdletBinding(DefaultParameterSetName='Default')]
  param(    
    [Alias('u')]
    [string] $Username
    ,
    [Alias('s')]
    [Parameter(ParameterSetName='Scripts')]
    [switch] $Scripts
    ,
    [Parameter(ParameterSetName='Desktop')]
    [Alias('d')]
    [switch] $Desktop
  )

  $targetFolder = 
    if ($Username) { 
      Join-Path (Split-Path -LiteralPath $HOME) $Username
    } else {
      $HOME
    }

  if ($Scripts) {
    $targetFolder += '\Desktop\Scripts'
  } elseif ($Desktop) {
    $targetFolder += '\Desktop'
  }

  Push-Location -LiteralPath $targetFolder
}

Sample calls:

folder             # change to current user's home folder

folder -d          # change to current user's desktop folder

folder -u jdoe -s  # change to jdoe's scripts folder

folder -?          # show help


Answered By - mklement0
Answer Checked By - Marilyn (WPSolving Volunteer)