Wednesday 18 March 2015

PowerShell change command prompt.

PowerShell change command prompt.





Yes, i am talking about the "PS c:\windows\>" looking thingie in the PowerShell console.
May be to hide the path, or if the prompt length has become too lengthy or from influence of a recent hollywood flick where the 'hacker' had his name instead of prompt, we would like to change it..;)

Here is how it could be done.....

PS C:\> # standard prompt which shows console is ready for input..

Windows Powershell, Changing the command prompt to Name.
Try 1:
        PS C:\> function prompt {"Hello > "}
        Hello >  # This becomes the prompt.



   
Windows Powershell, Changing the command prompt to Null.   
Try 2:
        PS C:\> function prompt {$null}
        PS> # this becomes the prompt you won't see the current working directory anymore.


       
Windows Powershell, Changing the command prompt to Hostname.   
Try 3:

        function prompt {"PS [$env:COMPUTERNAME]> "}
        # shows the hostname of the system


       
Windows Powershell, Changing the command prompt to date and Time.   
Try 4:
        function prompt {"$(get-date)> "}
        # shows date and time at prompt



Windows Powershell, Changing the command prompt color and attributes.   
Try 5:

        function Prompt
        {
            $promptString = "PS " + $(Get-Location) + ">"
            Write-Host $promptString -NoNewline -ForegroundColor Yellow
            return " "
        }
        # you could add any color for the prompt to reflect.
        # this could be customizable $promptString with anything we like.






Try 6:

function prompt
    {
        # to reset counter select and execute this > $global:LINE = 0
        $global:LINE=$global:LINE + 1
        $global:Loc = (get-location)
        'PS ' + $($loc) + ' ' + '(' + $($global:LINE) + ')' + $(if ($nestedpromptlevel -ge 1) { '>>' }) + ' > '

   }

         
           


 You could reset the line numbers with reseting the $global:LINE variable to 0 (Zero).
      
Happy Scripting..
        

Replace file content using PowerShell : Single and Multiple files.

Replace file content using PowerShell : Single and Multiple files.

Want to replace certain string using powershell?
Can be a single file or multiple files on you system..

Powershell offers a very simple way to achieve this and would considerably save your time for better things...

I would start of with editing single file first followed my multiple files..so open PowerShell console as Administrator and type along...

  • Editing single file:

Open Notepad and fill it with some random text.. or just copy paste this below text..

Server
Powershell
windows
Hosts
Active Directory
Exchange
Automation
Scripts
DSC
Hosting
Deployment
Administrator

save this as test.txt on your desktop, just ensure you are saving it correctly and not as test.txt.txt which may be appended while saving if not checked.

run the following cmdlet to verify.

change working directory to Desktop or where the test.txt was saved.

Get-Content .\test.txt



now we replace the "hosts" to "virtualhosts" by the below cmdlet.

(Get-Content -Path .\test.txt).Replace("Hosts","VirtualHosts") | Set-Content -Path .\test.txt












and check the content of the file now with the below cmdlet.

Get-Content .\test.txt




  • Editing Multiple files:

Now on the desktop., copy/ paste the test.txt file multiple times, may be 5 times.
So, we have five files .txt files which have data in it and we have to replace a specific string in all the five files at the same time.




All we need to do is improvise the above cmdlets into a script.

First we need a cmdlet to list all the txt files in desktop folder.

get-item c:\users\User1\Desktop\*.txt # this would output all the txt files and this has to be stored in a variable for script usage.

$InputFiles = get-item c:\users\User1\Desktop\*.txt # this would store all the file names with .txt extension.

Now we need a for loop, that would enable us to work with each file in the $InputFiles

$InputFiles = Get-Item "C:\Users\User1\Desktop\*.txt" # gathers the files ending with .txt
$OldString  = 'Powershell'   # string to be replaced with.
$NewString  = 'PowerShellisCool'    # new string
$InputFiles | ForEach {
    (Get-Content -Path $_.FullName).Replace($OldString,$NewString) | Set-Content -Path $_.FullName
}  # looping each file with its Fullname until all .txt files are covered and replaced with the new string.



Copy the above code to ps.ps1 and run it in powershell as administrator.


Listing of files in Desktop and showing the content in test.txt


 Ran the script ps.ps1 and checking the content in test.txt and other files.


Happy Scripting....

Tuesday 17 March 2015

Download files online using PowerShell.

Download files online using PowerShell,


Downloading files from internet may be like pdf's, exe's, doc's, excel etc from a script can be achieved with the below simple command.

(New-Object System.Net.WebClient).DownloadFile( 'http://websitewhereyouwanttodownload.com\filename.ext ', 'c:\temp\xx.xxx')

NOTE; ensure you give the name of the file completely with extension where you want to download locally using this command.

Example;

I would like to download the VLC player from the URL http://videolan.org/

I would use the below PowerShell cmdlet to get it downloaded locally.

(New-Object System.Net.WebClient).DownloadFile( 'http://get.videolan.org/vlc/2.2.0/win32/vlc-2.2.0-win32.exe', 'c:\temp\vlc-2.2.0-win32.exe')

This would download the setup file to your temp folder.




-have fun scripting.

Tuesday 3 March 2015

Use Powershell to send mail.

Use Powershell to send mail.

You need not bang your head with vbs scripts nor the executable/programs that send mail and cause worry to the security guys in your domain.


PowerShell has the most easiest way to send mail from console and not to mention we should be aware which one is the outgoing smtp server.

Send-MailMessage -To username@somedomain.com -Body 'Mail Body' -Subject 'Sending mail from PowerShell' -from'someuser@somedomain.com' -SmtpServer 'smtp.yoursmtpserver.com' -Attachment 'path of the attachment'


Isn't it easy? than downloading scripts that we didn't author and executing them on internal servers. :)

Give it a try...