Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Thread Tools Search this Thread
Old 07-02-2009
Join Date: Nov 2008
Posts: 1,066
Create a script using Powershell

I am completely new to Powershell. I want to create a script which will output the disk usage for my hard drives to a single text file and also the CPU and memory usage.

I have no idea from where to start with the Powershell, it all looks really complicated! Can someone shed light on this or explain me how to proceed?
Reply With Quote
Old 07-02-2009
XSI XSI is offline
Join Date: May 2008
Posts: 271
Re: Create a script using Powershell

For security reason MS has not associated ps1 extension with powershell instead it is associated with notepad. You may change that by making registry changes. In addition, by default the script won't be executed unless you issue command

Set-ExecutionPolicy Unrestricted

or sign your scripts. Read more about this by typing

get-help about_signing

in PowerShell.
Reply With Quote
Old 07-02-2009
Join Date: May 2008
Posts: 685
Re: Create a script using Powershell

Script Cmdlets are one of the coolest things about the newer version of PowerShell. A Script cmdlet allows you to use all of the variety of cmdlet parameter sets inside of PowerShell functions.

Since Script Cmdlets are PowerShell functions, and the PowerShell engine prefers to run functions rather than commands, you can use Script Cmdlets to override an existing cmdlet. You might want to do this to add or remove parameters from a cmdlet you use often. Also, you might just want to see what a cmdlet?s parameters look like in a script cmdlet, so you can go write your own.

Luckily, Powershell has a way to generate script cmdlets from an existing cmdlet. Below is a script cmdlet, called New-ScriptCmdlet, that I?ll use to create other script cmdlets.

You can use this to create new script cmdlets from existing cmdlets in a couple of different ways:

# Create a new PowerShell cmdlet from an existing cmdlet

Get-Command Get-Command | New-ScriptCmdlet Get-Command | Set-Content Get-Command.ps1

# Create a new PowerShell cmdlet from an existing cmdlet?s type

[Microsoft.PowerShell.Commands.GetProcessCommand] | New-ScriptCmdlet Get-Process | Set-Content Get-Process.ps1

# Creates a new PowerShell cmdlet from a random existing command and puts it into a file of the same name

Get-Command | Get-Random | Foreach-Object {
$cmdlet = $_
$scriptCmdlet = $cmdlet | New-ScriptCmdlet $cmdlet.Name
$scriptCmdlet | Set-Content "$($cmdlet.Name).ps1"

Hope this helps,
Reply With Quote
Old 07-02-2009
Join Date: May 2008
Posts: 945
Re: Create a script using Powershell

Here is a guide from Microsoft to let you start with Windows PowerShell. Read through these pages to get familiar with Windows PowerShell, and soon you?ll be driving around like a pro.

Reply With Quote
Old 07-02-2009
Join Date: Feb 2008
Posts: 194
Re: Create a script using Powershell

PowerShell Cmdlets (Command Lets)

First Meaning of PowerShell Cmdlet
Cmdlet has two meanings in Windows PowerShell; the first meaning of cmdlet is a synonym for a PowerShell script. A PowerShell cmdlet is a series of commands, usually more than one line, stored in a text file with a .ps1 extension. It is this scriplet meaning of cmdlet that I feature on this page.

Second Meaning of PowerShell Cmdlet
In PowerShell circles there appears to be a second meaning for the word cmdlet. For example Microsoft say, cmdlet means a built-in PowerShell command, which corresponds to a single verb-noun pair. Such a cmdlet is often accompanied by an alias, for example: get-Member with its alias of gm.

Advantages of Cmdlets
Once you have experimented with a few basic instructions at the PowerShell command line, you may wish to store your code in its own cmdlet file. The benefit is that you can replay these perfect lines of instructions later. This strategy becomes more and more useful as the code grow in complexity. My tactic is once a cmdlet achieves my basic objective, I call for 'SaveAs', and then use the copy for further development. Seven times out of ten the 'development' goes hopelessly wrong, at which point I am so grateful that I saved a working copy.

I hope that you are getting the idea of a cmdlet. Spend time perfecting the PowerShell commands, then save them in a text file. This technique saves you typing in lines of code and the command line, instead, you call the cmdlet by typing: dot slash and followed by its filename, for example .\memory. Incidentally, building PowerShell cmdlets fits in with my strategy of assembling scripts in sections.
Reply With Quote

  TechArena Community > Software > Software Development
Tags: ,

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads for: "Create a script using Powershell"
Thread Thread Starter Forum Replies Last Post
Powershell Script to Enable/Disable Network Connection windows_user Software Development 8 27-04-2012 09:35 PM
Running Powershell script with batch file with parameters? Jaslein Software Development 3 12-01-2011 03:36 AM
How to create Script for Always-on-top message? pushpendra Software Development 4 06-02-2010 06:30 AM
Powershell script to parse system logs in text file SADIQ Operating Systems 2 02-06-2009 07:05 PM
How to display powershell script output to XML StephanieRice Software Development 3 01-06-2009 02:10 PM

All times are GMT +5.5. The time now is 09:46 AM.