I am trying to copy files to any local drive with volume named "Data", but I am unsure how to parse out the drive letter for the copy.
This is the line I am using to grab the volumes:
$drive=Get-WmiObject -class Win32_logicaldisk |
Where-Object {$_.VolumeName -eq "Data"} |
select DeviceID
Then I want to do an xcopy from c:\temp to $drive/backupfolder (this is where it fails as $drive shows
DeviceId
--------
D:
$drive isn't a string. But an Object with the property DeviceId which is a string. You can either expand the property
$drive = Get-WmiObject -Class Win32_logicaldisk |
Where-Object {$_.VolumeName -eq "Data"} |
Select-Object -ExpandProperty DeviceID
Or expand the property this way:
$drive = (Get-WmiObject -Class Win32_logicaldisk | Where-Object {$_.VolumeName -eq "Data"}).DeviceID
Or reference the property on the variable:
"$($drive.DeviceID)\backupfolder"
Related
I am storing the following query value in a variable:
$unquotedPaths = Get-WmiObject -Class Win32_Service | Select-Object -Property Name,DisplayName,PathName,StartMode | Select-String "auto"
The problem starts when i print that variable becouse the variable takes from the query an object which is formed by hashtables like in this output:
PS C:\Users\pc> Get-WmiObject -Class Win32_Service | Select-Object -Property Name,DisplayName,PathName,StartMode | Select-String "auto"
#{Name=AGMService; DisplayName=Adobe Genuine Monitor Service; PathName="C:\Program Files (x86)\Common Files\Adobe\AdobeGCClient\AGMService.exe"; StartMode=Auto}
#{Name=AGSService; DisplayName=Adobe Genuine Software Integrity Service; PathName="C:\Program Files (x86)\Common Files\Adobe\AdobeGCClient\AGSService.exe"; StartMode=Auto}
#{Name=asComSvc; DisplayName=ASUS Com Service; PathName=C:\Program Files (x86)\ASUS\AXSP\1.01.02\atkexComSvc.exe; StartMode=Auto}
#{Name=AudioEndpointBuilder; DisplayName=Compilador de extremo de audio de Windows; PathName=C:\WINDOWS\System32\svchost.exe -k LocalSystemNetworkRestricted -p; StartMode=Auto}
How i can get and output like this:
Name DisplayName PathName Startmode
---------- ------------- ------------ ------------
ExampleName ExampleDisplayName C:\Example Auto
Select-String is meant to search and match patterns among strings and files, If you need to filter an object you can use Where-Object:
$unquotedPaths = Get-WmiObject -Class Win32_Service |
Where-Object StartMode -EQ Auto |
Select-Object -Property Name,DisplayName,PathName,StartMode
If the filtering required more complex logic you would need to change from Comparison Statement to Script Block, for example:
$unquotedPaths = Get-WmiObject -Class Win32_Service | Where-Object {
$_.StartMode -eq 'Auto' -and $_.State -eq 'Running'
} | Select-Object -Property Name,DisplayName,PathName,StartMode
How can I get a letter of first logical disk in PowerShell? I need to assign it to the variable.
So far I have:
$drive = Get-WmiObject -Class win32_volume
If you actually want logical disk information you should query win32_logicaldisk
Get-WmiObject win32_logicaldisk -filter "Drivetype=3"
3 being the Local Disk
If you only wanted the "first" you could pipe it into Select-Object
Get-WmiObject win32_logicaldisk -filter "Drivetype=3" | Select-Object -First 1
If you then still needed information from win32_volume you could do this
$firstDisk = Get-WmiObject win32_logicaldisk -filter "Drivetype=3" | Select-Object -First 1 -ExpandProperty caption
Get-WmiObject -Class win32_volume | Where-Object{$_.DriveLetter -eq $firstDisk}
Use this command:
Get-WmiObject win32_logicaldisk -ComputerName $pc -filter "Drivetype=3"
Depends what else you want to use it with of course, it should give you what you're looking for
Just for funs sake here is another proposal:
Get-PSDrive -PSProvider FileSystem | where {$_.Used -ne $null} | select -First 1
Matt´s answer is the way to go though
I am working on a script for my college assignment that basically gathers your computer information and outputs it to a .log file. I've written the script already but when it outputs the information to the .log file, the Installed Software Names, Installed Software GUIDs, and the name of all users in the computer are listed like this:
But I want it to look like this:
Anyway I can edit my script to make it like this? Here's my script:
#Checking For Log File
$LogLocation = "$Home\Desktop\"
$LogFile = "Baabbasi.log"
$TestPathResult = Test-Path $Home\Desktop\Baabbasi.log
If ($TestPathResult -eq "False") {New-Item -Path $LogLocation -Name $LogFile -ItemType File}
#The Process After
Clear-Host
$TodaysDate = Get-Date
$ComputerName = $env:ComputerName
$BiosName = Get-WMIObject Win32_BIOS | Select-Object -ExpandProperty Name
$BiosVersion = Get-WMIObject Win32_BIOS | Select-Object -ExpandProperty Version
$HDSizes = Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" | Select-Object #{Name="size(GB)";Expression={"{0:N2}" -f($_.size/1gb)}}
$TotalHDSize = ($HDSizes | Measure-Object "size(GB)" -Sum).Sum
$PhysicalMemory = (Get-WMIObject Win32_PhysicalMemory).Capacity
$PhysicalMemoryinGB = $PhysicalMemory/1gb
$OSVersion = (Get-WmiObject -Class Win32_OperatingSystem).Version
$OSName = $env:OS
$InstalledSoftwareNames = Get-WMIObject Win32_Product | Select-Object -ExpandProperty Name | Out-String
$InstalledSoftwareGUID = Get-WMIObject Win32_Product | Select-Object -ExpandProperty IdentifyingNumber| Out-String
$LatestHotfix = Get-Hotfix | select-object HotFixID,InstalledOn | Sort-Object InstalledON -descending | Select -first 1 | Select-Object -ExpandProperty HotfixID
$UserAccount = [Environment]::UserName
$AllUserAccounts = Get-WmiObject Win32_UserAccount | Select-Object -ExpandProperty Name | Out-String
Add-Content $Home\Desktop\Baabbasi.log "
Date: $TodaysDate
======================================================================
Computer Name: $ComputerName
================ ======================================================
BIOS Name: $BiosName
BIOS Version: $BiosVersion
HD Size: $TotalHDSize GB
RAM Size: $PhysicalMemoryinGB GB
Operating System: $OSName
Operating System Version: $OSVersion
Installed Software Name:
$InstalledSoftwareNames
Installed Software GUID:
$InstalledSoftwareGUID
Last Installed Hot Fix: $LatestHotfix
Name of Registered System User: $UserAccount
Names of All Registered System Users on the System:
$AlluserAccounts
========================================================================
========================================================================
"
Change the statement
$InstalledSoftwareGUID = Get-WMIObject Win32_Product |
Select-Object -ExpandProperty IdentifyingNumber| Out-String
to something like this:
$InstalledSoftwareGUID = Get-WMIObject Win32_Product |
Select-Object -ExpandProperty IdentifyingNumber |
% { (' ' * 20) + $_ } | Out-String
That will prepend each GUID with 20 spaces (adjust the number to your desired indention depth) before converting the list to a single string.
Using PowerShell, I want to include the computer name with some of the returned data of the WMI query. I've studied custom columns - need something like that - but I do not know how to pass the computer name into the next loop to be included in the resulting table. For example:
Get-WmiObject -Class Win32_LogicalDisk -ComputerName MailServer01
Actually, I'm in another look where $_ is the computer name:
Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_
This works great, but I want to include the ComputerName ($_) as part of the final output or report. Any ideas?
The computername is already there (as SystemName). It just isn't part of the default display properties.
Run
Get-WmiObject -Class Win32_LogicalDisk -ComputerName MailServer01 | format-list *
to see all of the properties of the returned objects.
The "__Server" property will always be available for the get-wmiobject objects
Also note:
"Beginning in Windows PowerShell 3.0, the __Server property of the object that Get-WmiObject returns has a PSComputerName alias. This makes it easier to include the source computer name in output and reports."
http://technet.microsoft.com/en-us/library/hh849824.aspx
Try this
Get-WMiObject -Class Win32_LogicalDisk -ComputerName $_ | Add-Member -MemberType NoteProperty -Name ComputerName -Value $_ -PassThru
Then you can output or manipulate your data anyway you see fit, and the properties you add via Add-Member will be available on that object as long as it exists.
Docs for the cmdlet are here
Yeah, I didn't realize there were more properties. Works great: __Server was the property I needed:
$myServers = #("server1", "server2", "server3")
"" > space.txt
$myServers | foreach-object {
write-host "Server: $_"
Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_ | ? { $_.DeviceID -notmatch " [AR]"} | Select -Property __Server, DeviceID, #{Name=’FreeSpaceMB’;Expression={$_.FreeSpace/1MB} } | Format-Table -AutoSize >> c:\space.txt
}
I am using following script to get disk space audit in our enterprise environment.
Everything works fine except that I don't know how to get those values presented in GB/MB.
Any idea?
$Computers = Get-Content -Path D:\DISKSPACE_audit\Servers.txt
Get-WmiObject Win32_LogicalDisk -ComputerName $Computers | Where-Object {
$_.DriveType -eq 3
} | Select-Object SystemName,DeviceID,FreeSpace,Size
Divide the value by 1GB (or 1MB):
$Computers = Get-Content "D:\DISKSPACE_audit\Servers.txt"
Get-WmiObject Win32_LogicalDisk -Computer $Computers -Filter 'DriveType = 3' |
Select-Object SystemName, DeviceID,
#{n='FreeSpace';e={[int]($_.FreeSpace/1GB)}},
#{n='Size';e={[int]($_.Size/1GB)}}