zipster1967 Posted May 1, 2016 Posted May 1, 2016 (edited) I would like my script to have certain settings that can be changed by the user if they want or left as is from the last time they changed them. (i.e. setting a default folder that is looked to every time a certain type of file is opened. Say user 1 has there word documents in C;\Users\Documents\Word\ while user 2 keeps their word documents on a USB drive F:\word Documents Is there a way to have my script open for user 1 to thier word document folder and user 2 open to their usb drive word documents folder? I was thinking about storing those variables in a text file that is different for every user but I am unsure how to import variables from a text file on script startup. Any suggestions on this? Also is there any good tutorials on scripting for powershell that help a newbie learn how to write scripts and gives different examples on using powershell comands in scripts? or maybe a compendium of powershell commands syntax for scripts? Edited May 1, 2016 by zipster1967
Matt_Renato Posted May 3, 2016 Posted May 3, 2016 (edited) Here is some simple code to get you started. It assumes you have the username and default path for each user stored in a CSV file with the headers Username and Path ### Enter path to CSV $CSVPath = "C:\Powershell\UserDetails.csv" ### Import the CSV $CSVImport = Import-CSV $CSVPath ### Grab the username of the logged on user, match it with the file path and set it to the variable $UserPath $UserPath = ($CSVImport | ?{$_.Username -eq $env:username}).Path ### Display the variable in the console Write-Output $UserPath You will need to change the path to the CSV to match the location on your system. The script sets the path value for the logged on user to the variable $UserPath, the last line just displays the variable in the PowerShell console. The built in help for PowerShell is pretty good and it can be accessed directly from the console. If you wanted to find out more about the Import-CSV cmdlet I've used in the above example you can just type Get-Help Import-CSV The are some good videos on YouTube too. Edited May 3, 2016 by Matt_Renato Fixed typo
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now