background preloader

Powershell

Facebook Twitter

Adam Driscoll’s Favorite PowerShell Tips & Tricks. I am a developer by trade and primarily use PowerShell to aid in my development practices.

Adam Driscoll’s Favorite PowerShell Tips & Tricks

The majority of the time I’m a .NET developer, which means I can use PowerShell to quickly prototype what I’m working on. This often requires me to run PowerShell as an administrator. 1. Running PowerShell as an Administrator You can easily right click on the PowerShell icon and select ‘Run as Administrator’ and the User Account Control (UAC) dialog will be presented. The RunAs verb is a special verb in Windows that enables processes to request elevation. 2. A lot of the things I develop are written in .NET 4.0. The problem with the app.config file is that it is buried in the system and requires an elevated text editor to be opened to make modifications. PowerShell Automatic LoggingThe Lonely Administrator. If you regularly download or look at the functions and scripts I post here, you’ll notice I often use Write-Verbose to indicate what is happening.

PowerShell Automatic LoggingThe Lonely Administrator

This comes in handy for troubleshooting. But often it would also be helpful to record as a log file of script activity. Unfortunately, you can’t pipe Write-Verbose to Out-File. So I came up with my own function called Write-Log that gives me the best of both worlds. I can get verbose messages and send those messages to a text file for logging. The function is relatively simple. The function takes two parameters, -Message and -Path. Write-Output "$(Get-Date) $Message" | Out-File -FilePath $LogFile -Append Output is always appended to the log file. #Pass on the message to Write-Verbose if -Verbose was detected Write-Verbose -Message $Message Now here’s the fun part. There are several ways then that you could use this. My TryMe function includes a parameter called -Log. Another way would be to turn on the logging variable and use the default log. Build an RDCMan (Remote Desktop Connection Manager) Config File - TechNet Script Center Repository.

Verified on the following platforms This script is tested on these platforms by the author.

Build an RDCMan (Remote Desktop Connection Manager) Config File - TechNet Script Center Repository

It is likely to work on other platforms as well. If you try it and find that it works on another platform, please add a note to the script discussion to let others know. Online peer support For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script. Disclaimer The sample scripts are not supported under any Microsoft standard support program or service. Windows PowerShell Profiles. When you add aliases, functions, and variables, you are actually adding them only to the current Windows PowerShell session.

Windows PowerShell Profiles

If you exit the session or close Windows PowerShell, the changes are lost. To retain these changes, you can create a Windows PowerShell profile and add the aliases, functions, and variables to the profiles. The profile is loaded every time that Windows PowerShell starts. To load a profile, your Windows PowerShell execution policy must permit you to load configuration files. If it does not, the attempt to load the profile fails and Windows PowerShell displays an error message.

Understanding the Profiles You can have four different profiles in Windows PowerShell. %windir%\system32\WindowsPowerShell\v1.0\profile.ps1 This profile applies to all users and all shells. Creating a Profile When you create or import variables, aliases, or functions, or add a Windows PowerShell snap-in, these elements are added only to the current session.

To open the profile in Notepad, type: Yet Another Countdown Timer Using PowerShell. So I have been literally sitting on this script for about a year now and am finally looking to publish it after seeing Josh Atwell’s awesome post about writing a countdown timer in the PowerShell console and using it as motivation to stop being lazy with it.

Yet Another Countdown Timer Using PowerShell

I figured I would take a break from doing some judging in this years Scripting Games to finish up this little ‘project’ and get it out of the way. My take on this doesn’t use the console to display a countdown timer, but instead uses WPF to display the countdown timer. Like I said, I actually had this back around July when I wanted to have a countdown of some sort to let me know when the Omaha Half Marathon was happening and whipped it together with the plan of cleaning it up later on and blogging about it. Well, fast forward several months and here we go! My requirements for this countdown timer is that had to consist of the following things: Some code used for this here: Logging All Powershell Commands and Output « Scott Jaworski's Blog. There have been a number of occasions where I have accidentally closed out of powershell, and had to retype a script that took some time to create, or wanted to go back to see what commands I had already run.

Logging All Powershell Commands and Output « Scott Jaworski's Blog

Usually the up Arrow Key is sufficient for this, however what about commands that you ran a few days ago? This post will walk you through setting up a powershell profile to automatically log all activity within powershell. 1. Create the Folder Path for the script output file. Mine is C:\PowershellLog.