Varonis announces strategic partnership with Microsoft to accelerate the secure adoption of Copilot.

Learn more

Getting Started with PowerShell Option Inputs

PowerShell is the dominant method of automating tasks and scripting changes for Windows sysadmins. This article covers getting started with some basic PowerShell usage and how to pass optional customization...
Michael Buckbee
1 min read
Published November 17, 2014
Last updated February 21, 2022

PowerShell is the dominant method of automating tasks and scripting changes for Windows sysadmins. This article covers getting started with some basic PowerShell usage and how to pass optional customization values into scripts.

At the command prompt arguments

Arguments

Arguments refer to the options that are passed to a script that can then be acted upon. A common usage pattern with Varonis products is that a rule is triggered which in turn calls a script – passing along the relevant details as arguments.

Example: When a DatAlert rule for X is bypassed the DatAlert passes [USER] [FOLDER_PATH] to the specified script.

This lets you codify your responses to user actions and can help make your life as a sysadmin easier.

PowerShell scripts are launched and edited from the PowerShell Interactive Script Editors (ISE)

  1. Write-Host "Arguments:" $args.Length;
  2. foreach ($arg in $args){
  3. Write-Host "Argument: $arg";
  4. }
Write-Host "Arguments:" $args.Length;
foreach ($arg in $args){
Write-Host "Argument: $arg";
}

Arguments are very flexible in that you don’t need to know how many will be passed in ahead of time. This is useful for situations where you’ll be taking the same action and applying it to a set of passed in things (ex. a series of usernames to grant or remove file access to). Arguments are accessed within your script in the order they were passed in (denoted in array syntax).

$arg[0] returns the value of the first argument $arg[1] returns the second.

 

Parameters

Parameters are arguments more formal big brother: they help make your scripts easier to maintain by explicitly assigning incoming options to variables.

To function, the parameters definition must be the first executable line in the script.

Example parameters block:

  1. Param(
  2. [string]$userName,
  3. [string]$filePath
  4. )
Param(
[string]$userName,
[string]$filePath
)

Further into your script you can then reference the $userName and $filePath variables where needed.

What you should do now

Below are three ways we can help you begin your journey to reducing data risk at your company:

  1. Schedule a demo session with us, where we can show you around, answer your questions, and help you see if Varonis is right for you.
  2. Download our free report and learn the risks associated with SaaS data exposure.
  3. Share this blog post with someone you know who'd enjoy reading it. Share it with them via email, LinkedIn, Reddit, or Facebook.

Try Varonis free.

Get a detailed data risk report based on your company’s data.
Deploys in minutes.

Keep reading

Varonis tackles hundreds of use cases, making it the ultimate platform to stop data breaches and ensure compliance.

powershell-variable-scope-guide:-using-scope-in-scripts-and-modules
PowerShell Variable Scope Guide: Using Scope in Scripts and Modules
PowerShell variable scopes can cause confusion in writing scripts and functions. This post will cover PowerShell scopes in relation to scripts and modules.
windows-powershell-scripting-tutorial-for-beginners
Windows PowerShell Scripting Tutorial For Beginners
New to PowerShell scripting? Explore these scripting tutorials to learn to write and execute basic scripts, PowerShell cmdlets, aliases, pipes and more.
how-to-use-powershell-objects-and-data-piping
How to use PowerShell Objects and Data Piping
This article is a text version of a lesson from our PowerShell and Active Directory Essentials video course (use code ‘blog’ for free access). The course has proven to be...
powershell-for-pentesters:-scripts,-examples-and-tips
PowerShell for Pentesters: Scripts, Examples and Tips
This PowerShell for Pentesters' guide covers running commands, coding, tutorials and examples as well as the benefits of pentesting with PowerShell.