powershell add custom parameter for scheduled run?

Eaven HUANG 2,181 Reputation points
2023-02-01T08:16:02.2633333+00:00

Dear Experts,

I have a small script to update some users attributes from csv. Every time when I wanted to use new csv file, I have to change the csv path inside the script. My script starts with $ADUsers = Import-Csv 'c:\test.csv'

My question is - how can I revise the script so I can maybe next time, run .\myscript.ps1 -path 'c:\test.csv'?

I can't remember where I saw this before but I can't find the correct direction via googling now:(

Here is an example of my small scripts



Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,627 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 35,511 Reputation points
    2023-02-01T13:52:48.3133333+00:00

    Add a parameter.

    
    param (
        [string]$path = ''                            # analyze this csv 
     )
    
    if ($path -eq '') {
        "Please specify the path to a csv to analyze."
        return 
    }
    if (!(Test-Path $path)) {
        "This file does not exist $path "
        return 
    }
    try {
        $csv = Import-Csv $path -ErrorAction Stop
        $csv.count
    } catch {
        "Error, unable to access $path"
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.